Q:

Write a JavaScript program to find the maximal difference between any two adjacent elements of a given array of integers

0

Write a JavaScript program to find the maximal difference between any two adjacent elements 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 max_difference(arr) {
	var max = -1;
    var temp;
	for (var i = 0; i < arr.length - 1; i++)
      {
		temp = Math.abs(arr[i] - arr[i + 1]);
		max = Math.max(max, temp);
	  }
	return max;
}

console.log(max_difference([1, 2, 3, 8, 9]))
console.log(max_difference([1, 2, 3, 18, 9]))
console.log(max_difference([13, 2, 3, 8, 9]))

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