Originally Posted by
Arch_Angel
Oops. Remove the quotes around the 2 and the 1. What javascript does with the values depends on how you store the values.
You stored them as strings (the quotes around them), so when you try to add them, javascript puts them together to get 21.
Remove the quotes and it will treat them as numbers.
Code:
<script type="text/javascript">
<!--
var myArray = new Array(20);
myArray[0] = 2;
myArray[1] = 1;
var i = 0;
var j = 1;
for (var x = 2; x < myArray.length; x++)
{
myArray[x] = myArray[i] + myArray[j];
document.write(myArray[x]);
i++;
j++;
}
//-->
</script>
Thanks!!!
i figured out another way as well
Code:
<script type="text/javascript">
<!--
var myArray = new Array(20);
myArray[0] = "2";
myArray[1] = "1";
for (var i = 2; i < myArray.length-1; i++)
{
myArray[i] = parseInt(myArray[i-1]) + parseInt(myArray[i-2]);
document.write( +myArray[i] + ',' );
}
//-->
</script>
Thanks again... first time with Javascript so was aplying C/C++/C# knowledge...
OT: We need a rep system here!!!