Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to create a loop in a function with another function?
    primarykey
    data
    text
    <p>I'm new to Java and I'm doing a uni course. I've been asked to design three functions.I have to find the difference between each adjacent numbers in an array, another to total the array and the last one to calculate the difference using the other functions then write a programme. I'm totally lost on the last function and my tutor has gone away on hols. Here is the code I have done so far. I don't want people doing the code for me but if anyone can advice me what I need to do I would appreciate your advice. I'm not sure how to loop the difference function into the array and store it into the new array I have made. If anyone could explain where I am going wrong I would love to hear from you!</p> <pre><code>var numberArray = [10,9,3,12]; // function difference will find the highest value of the two numbers,find the difference between them and return the value. function difference(firstNumber, secondNumber) { if (firstNumber &gt; secondNumber) { return (firstNumber - secondNumber); } else { return (secondNumber - firstNumber); } } // function sum will add the total numbers in the array and return the sum of numbers. function sum(numberArray) { numberTotal = 0 for (var total = 0; total &lt; numberArray.length; total = total + 1) { numberTotal = numberTotal + numberArray[total] } { return numberTotal } /*code the process that calculates a new array containing the differences between all the pairs of adjacent numbers, using the difference() function you have already written. This function should be named calculateDifferences() and should accept an array numberArray. The function should first create a new empty array of the same size as numberArray It should then calculate the differences between the pairs of adjacent numbers, using the difference() function, and store them in the new array. Finally, the function should return the new array. The calculation of the differences should be done in a loop, at each step finding the difference between each array element and the next one along in the array, all except for the last difference, which must be dealt with as a special case, because after the last element we have to wrap round to the start again. So the final difference is between the last and first elements in the array.*/ function calculateDifferences() var createArray = new Array (numberArray.length); { createArray = 0; for (var c = 0; c &lt; numberArray.length; c = c + 1) { createArray = difference(numberArray[c]); } { return createArray } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload