I have a programme in which I have written three functions, difference (that calculates the difference in numbers between arrays) sum (that totals the array up) and calculate difference which uses the difference function to determine what the next array should be).
All these functions are working but I'm having problems getting the while loop to work in the main programme and I can't see where I am going wrong!
If anyone could point me in the right direction then I would appreciate it. The programme is supposed to write out the initial array. It then calculates the new array and writes it out (which it does so far). I then need to loop it round so that while ever the sum is not 0 the programme runs and counts how many times it takes to repeat the process in order to reach 0. I thought that I would have to set the values of the numberArray back to the new figures from calculate difference and then clear the calculate difference array out. 开发者_如何学GoI've been working at this for days and I'm no closer to working out what I need to do. I'm not wanting people to give me the answer as it is for my coursework but I would like some guidance as to where I am going wrong.
function difference(firstNumber, secondNumber)
{
if (firstNumber > secondNumber)
{
return (firstNumber - secondNumber);
}
else
{
return (secondNumber - firstNumber);
}
}
function sum(numberArray)
{
numberTotal = 0
for (var total = 0; total < numberArray.length; total = total + 1)
{
numberTotal = numberTotal + numberArray[total]
}
{
return numberTotal
}
}
function calculateDifferences()
{
var createArray = new Array(numberArray.length);
for (var c = 0; c < numberArray.length - 1 ; c = c + 1)
{
createArray[c] = difference(numberArray[c],numberArray[c+1]);
}
{
createArray[numberArray.length - 1] = difference(numberArray[0],numberArray[numberArray.length - 1]);
}
{
return createArray;
}
}
var numberArray = [16,14,4,5];//initial numbers to start with
document.write(numberArray +'<BR>');//writes out initial numbers
sum(numberArray);// checks to see if sum total = 0
var count = 0;// delcares the counter to 0
while(sum(numberArray) > 0)// runs the programme while sum is not 0
{
count = count + 1;// counts how many times looped
calculateDifferences(numberArray);//calculates the new numbers from numberArray
document.write (calculateDifferences() + '<BR>');//writes out new numbers
calculateDifferences = numberArray;// sets the numberArray to new figures
calculateDifferences() = 0;//clears array for next calculate
sum (numberArray);//checks the condition again
}
document.write ( 'interations taken = ' + count + '<BR>');//if sum 0 then programme finishes by writing out how many times it took to get to 0
In the third line of your loop you callcalculateDifferences() again without passing in a parameter. Maybe in the second line, save the value returned by calculateDifferences(numberArray) into a variable, and then write out the variable.
Edit: also, you can't clear an array by setting it equal to 0
Your problem calls are these:
calculateDifferences = numberArray;// sets the numberArray to new figures
calculateDifferences() = 0;//clears array for next calculate
They don't make sense.
The first call tries to set the function to equal your array?
And the second call is setting the function equal to 0?
You can use your function to perform calculations and return values, but you shouldn't be setting your function to be equal to something.
What you want to do is call calculationsDifferences once and store it's output in a variable. After that, you can just reassign your numberArray variable to point to the new array. You don't need to worry about cleaning it out first, either.
Also, things you are doing wrong but won't cause your code to break:
- Your calculateDifferences does not require any arguments. So you don't need to pass it numberArray.
- You don't need the sum(numberArray) call at the end of your loop, your loop condition will check that every time. Especially you don't need that since you're not using it's return value for anything.
Four things. First, the extra braces in some of your functions don't do anything. Unlike C, javascript does not have brace scope, only function scope. Removing them makes the code more readable in my opinion.
Second, the difference function should really be just Math.abs(firstNumber-secondNumber)
. Which is not only shorter but more readable as well.
Third, the general structure you are aiming for is something like:
while( checkSomeCondition(myArray) ) {
myArray = doSomethingWith(myArray);
}
don't worry about freeing the old data for the array. It will automatically be garbage collected when nothing refers to it anymore.
Fourth, personally I would modify the calculateDifferences
function to take an array as an argument rather than passing the array as a global variable.
createArray[numberArray.length - 1] = difference(numberArray[0],numberArray[numberArray.length - 1]); i think this is the problem,
try createArray[3] = difference(numberArray[0],numberArray[3]);
精彩评论