2

I am trying to solve one of the problem on codewars. The problem is as below:

In mathematics, the Fibonacci numbers are in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number in it is the sum of the two preceding ones:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... Implement function getFibonacci(n) which will return the string representation of number on n position on the Fibonacci sequence.

I have worked out my codes, but my code can only past the test when the tested numbers are small and failed all tests when large numbers involved and getting the error message like "Expected: '30010821454963453907530667147829489881', instead got: '3.001082145496346e+37'". In addition, I have tried to add toNotExponential() or expandExponential() at the end of my return statement to help show the full number, however, either of the code works as I keep getting errors saying"toNotExponential() is not a function" or " expandExponential() is not a function". Not sure what to do, please help. My code as below...

    function getFibonacci(n) {
    var arr= [1,1];
    var len = arr.length;
    while(len<n){
    arr.push(arr[len-1]+arr[len-2]);
    len++;
   }
   return (arr[len-1]+"").toNotExponential();
   }
PreetyK
  • 455
  • 5
  • 14
Sen123
  • 97
  • 1
  • 8
  • Take a look at [this question](https://stackoverflow.com/questions/1685680/how-to-avoid-scientific-notation-for-large-numbers-in-javascript) – lztachyon Oct 04 '17 at 05:54
  • For numbers greater than [*Number.MAX_SAFE_INTEGER*](http://ecma-international.org/ecma-262/8.0/#sec-number.max_safe_integer) `9007199254740991` you'll need to use a library, or write one yourself. See [*Javascript summing large integers*](https://stackoverflow.com/questions/4557509/javascript-summing-large-integers) for suggestions. BTW, there is no `int` type in javascript, there are just numbers. – RobG Oct 04 '17 at 05:58
  • You can store the number as a string, and use thousands as a delimiter, where the length of the number is determined by the maximum possible `.length` of a string, without using E-notation, see [How do I add 1 to a big integer represented as a string in JavaScript?](https://stackoverflow.com/questions/43614407/how-do-i-add-1-to-a-big-integer-represented-as-a-string-in-javascript/) – guest271314 Oct 04 '17 at 06:03

1 Answers1

0

You can use the sum of the parts of a given number as string to perform mathematical procedures. Consider that each three digits of a number where the .length of the number as a string is greater than 3 can be viewed as thousands, where adding 1 to 999 would result in 1000. We can then state that adding N to 1000 would result in 2000 if N is equal to 1000. Thus, we can use the indexes of a string to determine which precise thousands we are iterating within the string, and select groups of 3 to determine if one or more numbers within an adjacent set of 3 should be recalculated, and displayed as numbers without E-notation. See How do I add 1 to a big integer represented as a string in JavaScript?.

guest271314
  • 1
  • 15
  • 104
  • 177