views:

449

answers:

6

Hi, 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. I'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
A: 

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

chama
I have a new Array called createArray that is returned when calculate difference is run. In order to get this to write out I just have to call back calculateDifferences and it will write out the new Array that I created. Would I have to clear the array out the next time it is used? If so how would I go about doing this if 0 will not clear it out? Thanks
newuser
Honestly, I've never dealt with javascript, so sorry, no idea.
chama
Thanks for trying to help I really appreciate your time. I will have a google to see if I can find out how to clear them. x
newuser
A: 

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:

  1. Your calculateDifferences does not require any arguments. So you don't need to pass it numberArray.
  2. 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.
Roman Stolper
Thanks. I understand now what I have been doing wrong when it comes to calling back my functions and not to use the sum(numberArray) at the end. I'm just unsure as to how I will reassign my variable createArray that calculateArray created in order for the loop to use them values the next time it loops. I thought the above would do that but obviously I went about it the wrong way. Any guidance would be appreciated.
newuser
Sure. Just do this:`myNewArray = calculateArray();``numberArray=myNewArray;`myNewArray will hold the contents of the array that calculateArray() returns (i.e. the contents of createArray -- notice that you don't have to use the name createArray to hold the output, createArray is just a local variable name that your function is using). Then the next line of code sets your numberArray variable equal to the new values.
Roman Stolper
A: 

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.

slebetman
A: 
Bebo
Answers should not be questions. Either post a comment or start your own question.
Jimmy Cuadra
A: 

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]);

trebor
A: 

I am not interested in programming because I fell it boring and This is also boring for me from my first day in my second semester, so I cant give complete answer but i know something about while loop is that it is executed when condition is true.

when the condition given in the while loop is not true then it will not be executed. another advantage of while loop is It looks more graceful. syntax of while loop is while(condition) {

                                }
TARIQ KANHER