help with javascript

February 3, 2010 at 01:51:05
Specs: Windows Vista
need help with this it keeps coming back as NaN, im trying to find the difference between 2 numbers in the array i,e, array[1,2,3,4,5,6]
im trying to find difference with 1-2,2-3,3-4,4-5,5-6,6-1 can anybody help here is the code that ive put together so far but i think i messed up.

function difference()
{
firstNumber = numberArray;
secondNumber = numberArray;
return firstNumber[numberArray.length] - secondNumber[numberArray.lemght];
}

var numberArray = [1,2,3,4,5,6];

document.write(numberArray + '
' +difference() +'
');


See More: help with javascript

Report •

#1
February 3, 2010 at 05:59:01
it keeps coming back as NaN
length() will return 6. Your last element is 5.

im trying to find difference with 1-2,2-3,3-4,4-5,5-6,6-1
Then you'd need a for loop.


Report •

#2
February 3, 2010 at 06:39:17
tryed a for loop and it froze me out of the pc

Report •

#3
February 3, 2010 at 06:40:44
Well, I can't critique your code if I can't see it.

Report •

Related Solutions

#4
February 3, 2010 at 06:56:16
var numberArray = [1,2,3,4,5,6] ;
var diffy = new Array(6);
for (var diff = 0; diff < numberArray.length; diff = diff + 1)
{
diffy[diff] = numberArray[diff] - numberArray[diff + 1 ] ;
}
for (var diffy = 0; diffy < diffy.length; diffy = diffy + 1)
{
document.write(numberArray[diffy] + ',');
}

Report •

#5
February 3, 2010 at 09:45:17
for (var diffy = 0; diffy < diffy.length; diffy = diffy + 1)
Check your variable names. You're trying to use the array's name, to assign a new variable.

diffy[diff] = numberArray[diff] - numberArray[diff + 1 ] ;

diffy[5] = numberArray[5] - numberArray[6]
diffy[5] = 6 - undefined
diffy[5] = NaN
Use modulus to fix the last loop iteration:
diffy[diff] = numberArray[diff] - numberArray[(diff + 1) % numberArray.length];


Report •

Ask Question