0

I'm creating a function to do some calculus and the result will be displayed on the navigator console using console.log ,but I'm facing a problem with the Array result which succeed on adding the first result to my main Array using arr.push(result) but the rest of the occurrence are displayed as NaN

this is the code :

function  Fibo() {
    var arr = [1, 2, 3];
    var result = 0;
    while(result < 4000000) {
          result = arr[arr.indexOf(arr.length)] + arr[arr.indexOf(arr.length - 1)];
          arr.push(result);
          console.log(arr);
          result = 0
    } 
}

Fibo();

This is the result :

[1, 2, 3, 5, NaN]
[1, 2, 3, 5, NaN, NaN]
[1, 2, 3, 5, NaN, NaN, NaN]
[1, 2, 3, 5, NaN, NaN, NaN, NaN]
[1, 2, 3, 5, NaN, NaN, NaN, NaN, NaN]
[1, 2, 3, 5, NaN, NaN, NaN, NaN, NaN, NaN]
[1, 2, 3, 5, NaN, NaN, NaN, NaN, NaN, NaN, NaN]
[1, 2, 3, 5, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN]
[1, 2, 3, 5, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN]
Eddy
  • 109
  • 1
  • 9
  • 1
    Summing `undefined` with a number or another `undefined` results `NaN`. – Teemu Feb 10 '15 at 23:34
  • [Also good to know](http://stackoverflow.com/questions/8331492/javascript-fibonacci/8331546#8331546) ... – Teemu Feb 10 '15 at 23:47

4 Answers4

1

There are two main issues:

  1. indexOf isn't the right method to use. Following the code step-by-step...

    arr[arr.indexOf(arr.length)] + arr[arr.indexOf(arr.length - 1)]
    arr[arr.indexOf(3)] + arr[arr.indexOf(2)]
    arr[2] + arr[1]
    3 + 2
    5
    
    arr[arr.indexOf(arr.length)] + arr[arr.indexOf(arr.length - 1)];
    arr[arr.indexOf(4)] + arr[arr.indexOf(3)]
    arr[-1] + arr[2]
    undefined + 2
    NaN
    
  2. Setting result to 0 at the end of the while-loop will cause the while-loop to run in an infinite loop! My guess is that you were clearing out variables, but be careful!

The below code fixes both issues (result is lower just as a sanity-check)

function Fibo() {
    var arr = [1, 2, 3];
    var result = 0;
    while(result < 400) {
          result = arr[arr.length-1] + arr[arr.length-2];
          if (isNaN(result)) { return }
          arr.push(result);
          console.log(arr);
    } 
}

Fibo();
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
1

When you add the 5 to your array it breaks, Here's what's happeing. Your array is now of length 4.
There is no 4 in your array so arr.indexOf(arr.length) is -1.
arr[-1] is undefined
undefined -1 is NaN

Instead of using indexOf you just want to add the last 2 number in the list and ad that to the end.

function  Fibo() {
var arr = [1, 2];
var result = 0;
while(result < 100) {
      result = arr[arr.length -1] + arr[arr.length -2];
      arr.push(result);
      console.log(arr);
  }
}

Fibo();
Schechter
  • 224
  • 2
  • 5
0

Try adding a NaN check before your push

function  Fibo() {
    var arr = [1, 2, 3];
    var result = 0;
    while(result < 4000000) {
          result = arr[arr.indexOf(arr.length)] + arr[arr.indexOf(arr.length - 1)];
          if (isNaN(result)) { return }
          arr.push(result);
          console.log(arr);
          result = 0
    } 
}
NominalAeon
  • 593
  • 5
  • 23
0

On the second time around the loop, arr.length === 4. That's not an element of your array, so that's your first problem. You might want to use arr[arr.length - 1] instead.

Foobie Bletch
  • 300
  • 2
  • 8