As a Java newbie enrolled in a university course, I have been tasked with designing three functions. The first function is to find the difference between each adjacent pair of numbers in an array, the second function is to calculate the total sum of all numbers in the array, and the third function involves using the previous two functions to write a program. However, I am struggling with implementing the final function as my tutor is currently on holiday. Below is the code that I have managed to write so far. While I don't want someone to do the coding for me, any advice on how to proceed would be greatly appreciated. Specifically, I need help on looping the difference function through the array and storing the results in a new array. If anyone could point out where I might be going wrong, I would be very grateful!
var numberArray = [10, 9, 3, 12];
function difference(firstNumber, secondNumber) {
if (firstNumber > secondNumber) {
return (firstNumber - secondNumber);
} else {
return (secondNumber - firstNumber);
}
}
function sum(numberArray) {
var numberTotal = 0;
for (var i = 0; i < numberArray.length; i++) {
numberTotal += numberArray[i];
}
return numberTotal;
}
function calculateDifferences() {
var differencesArray = new Array(numberArray.length);
for (var j = 0; j < numberArray.length; j++) {
if (j === numberArray.length - 1) {
differencesArray[j] = difference(numberArray[j], numberArray[0]);
} else {
differencesArray[j] = difference(numberArray[j], numberArray[j + 1]);
}
}
return differencesArray;
}