Q:

Write a JavaScript program to compute the sum of absolute differences of consecutive numbers of a given array of integers

0

Write a JavaScript program to compute the sum of absolute differences of consecutive numbers of a given array of integers.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

function sum_adjacent_difference(arr) {
	var result = 0;
	for (var i = 1; i < arr.length; i++) {
		result += Math.abs(arr[i] - arr[i - 1]);
	}
	return result;
}

console.log(sum_adjacent_difference([1, 2, 3, 2, -5]));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now