TransWikia.com

WHY THE FUNCTION IS RETURNING NaN

Stack Overflow Asked by user13986133 on December 22, 2021

When I try to run the function below, the line of code is returning Nan, when it’s supposed to return a number.

 var next = list[last]+list[before];

This is the full code:

function fibonacciGenerator(n) {
  var list = [0, 1];
  if (n === 0) {
    return [];
  } else if (n === 1) {
    return [0]
  } else {
    while (n > 1) {
      var last = list.length;
      var before = last - 1;
      var next = list[last] + list[before];
      list.push(next);
      n -= 1;
      return list;
    }
  }
}

console.log(fibonacciGenerator(8));

3 Answers

You just using index out of array range, an array has index range from 0 to length - 1, so array[length] + ... will return NaN

But, Something i can change in your code:

  1. Try to think about calculating fibo(n) from fibo(n-1), not just recalculate from beginning.

Something like this look better:

var fibo = function(n){
  // some funky code to check fatal input
  ....

  // init special values
  if(n==0) return []; 
  if(n==1) return [1]; 
  if(n==2) return [1, 1];

  // get previous state 
  var arr = fibo(n-1); 
  var len = arr.length; 
  arr.push(arr[len-2] + arr[len-1]); 
  return arr;
}
  1. You will hear your computer sound like a helicopter if you must use this function many times. So if you need re-use this, you need something for caching calculated values. Ex:
epic_history = {
  1: 1,
  2: 1,
  3: 2,
  4: 3,
  5: 5,
  .....
}

Answered by takid1412 on December 22, 2021

Because you're trying to access list[last], which is list[list.length]. However, because a list of length 1 has a single item at index 0, you're getting undefined. So, use list.length - 1:

function fibonacciGenerator(n) {
  var list = [0, 1];
  if (n === 0) {
    return [];
  } else if (n === 1) {
    return [0]
  } else {
    while (n > 1) {
      var last = list.length - 1;
      var before = last - 1;
      var next = list[last] + list[before];
      list.push(next);
      n -= 1;
      return list;
    }
  }
}

console.log(fibonacciGenerator(8));

(Also, your function has a small issue - return list runs as soon as the first loop iteration has completed. Move it outside of the loop:

function fibonacciGenerator(n) {
  var list = [0, 1];
  if (n === 0) {
    return [];
  } else if (n === 1) {
    return [0]
  } else {
    while (n > 1) {
      var last = list.length - 1;
      var before = last - 1;
      var next = list[last] + list[before];
      list.push(next);
      n -= 1;
    }
    return list;
  }
}

console.log(fibonacciGenerator(8));

Basically, the way it works in JavaScript is that array[array.length] will always be undefined. It starts at index 0 - so array.length is always one more than the index of the last element:

let nextElementIndex = array.length; // The next element to be added to the array will have the index of nextElementIndex, or array.length AT THIS CURRENT MOMENT
array.push(1); // `1` has the index nextElementIndex
nextElementIndex == array.length; // False, because there's a new element in the array, so the array's length has updated to nextElementIndex + 1.

Answered by Jack Bashford on December 22, 2021

It is because your last = 2, which makes list[last] = list[2] while it only has 2 elements that is list[0] and list[1]. Therefore, you get undefined out of this one, and when undefined + list[before], it gets NaN.

Answered by holydragon on December 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP