hi heed help with this im trying to get a function difference to work
all i had was the arry and thats it
array is 1,2,3,4,5,6 and i want to find the difference of 1,2 2,3 3,4 4,5 5,6 6,1 and return a number 1,2 return 1 2,3 return 1 ect
can you help

``````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] + ',');
}``````

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

2
Contributors
1
4
Views
8 Years
Discussion Span
Last Post by Graphix

>> Line 2 and 7 - You already defined "diffy" as an array in line 2, you can not declare it again.

Use the following script:

``````<script type="text/javascript">
var numberArray = [696,039,231,909025,5325,326,983] ;
var differenceArray = new Array();
for (var s = 0; s < numberArray.length; s++)
{
differenceArray[s] = numberArray[s + 1] - numberArray[s];
}
document.write("The original numbers:<br /><br />");
for (var d = 0; d < numberArray.length; d++)
{
if (d != numberArray.length - 1) {
document.write(numberArray[d] + ', ');
} else {
document.write(numberArray[d]);
}
}
document.write("<br /><br />The differences between the numbers:<br /><br />");
for (var a = 0; a < differenceArray.length; a++)
{
if (a != differenceArray.length - 1) {
document.write("Difference between " + numberArray[a] + " and " + numberArray[a + 1] + " is " + differenceArray[a] + "<br />");
}
}
</script>``````

~G

Edited by Graphix: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.