0

Task content: The fib() function is to implement a formal formula for the Fibonacci sequence. The input parameter is the number of words returned in the array after finishing the function. The function should check the correctness of the input data.

I've implement fibonacci as below, but have no idea, how to make input parameter the amount of returned numbers from the same function. When I tried to use recurrency function didn't work at all (was giving null or undefined result). Also, function returns always one number for me (n), so how it can become an array?

function fib(n) {


  if (n <= 1) {
    return n;
  } else {
    n = (n - 2) + (n - 1);
    return n
  }
}



console.log(fib(8))

1 Answers1

1

If you want to input separate number inputs.map(c=> fib(c));

You can use var arr = Array.from({length: 10}, (v, k) => fib(k)); to create array of sequence fib number.

Or use for loop like this

function fib(n) {
  if (n <= 1) {
    return n;
  } else {
    n = fib(n - 2) + fib(n - 1);
    return n
  }
}

// if you want to get sequence fib
var arr = Array.from({length: 10}, (v, k) => fib(k));
console.log(arr)

// use map if you input separate number
console.log('use map');
var inputs = [1, 4, 5, 8];
var outputs = inputs.map(c=> fib(c));
console.log(outputs)

// use for loop
var outputloops = [];
console.log('use for loop');
for(i =0; i< inputs.length; i++){
   outputloops.push(fib(inputs[i]));
}

console.log(outputloops);

function fibwitharray(arr) {
  var outputloops = [];
   
   for(i =0; i< inputs.length; i++){
      outputloops.push(fib(inputs[i]));
   }
   return outputloops;
}

function fib(n) {
  if (n <= 1) {
    return n;
  } else {
    n = fib(n - 2) + fib(n - 1);
    return n
  }
}

// if you want to get sequence fib
var arr = Array.from({length: 10}, (v, k) => fib(k));
console.log(arr)

// use map if you input separate number
console.log('use map');
var inputs = [1, 4, 5, 8];
var outputs = inputs.map(c=> fib(c));
console.log(outputs)

// use for loop
var outputloops = [];
console.log('use for loop');
for(i =0; i< inputs.length; i++){
   outputloops.push(fib(inputs[i]));
}

console.log(outputloops);

And this is exact your requirement

function fib(n) {

  const result = [0, 1];
  for (var i = 2; i < n; i++) {
    result.push(result[i-2] + result[i-1]);
  }
  return result; // or result[n-1] if you want to get the nth term

}

console.log(fib(8)); 
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
  • could you explain what is v and k here? and how do I make function to take this array as an beggining input at first place –  Apr 30 '19 at 00:00
  • you can refer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from – Hien Nguyen Apr 30 '19 at 00:03
  • you should not do like this, because your fib method is recusive, i updated your fib function correctly now – Hien Nguyen Apr 30 '19 at 00:11
  • may I ask you to rewrite this array.from function traditional way not arrow? maybe it will help me understand what's going on here, cause I think I don't get it and doing it by myself is too complicated for me –  Apr 30 '19 at 00:30
  • I updated answer use traditional way with foo loop – Hien Nguyen Apr 30 '19 at 00:38
  • @powerpuffgabi is there still any problem? if the answer help you resolve problem you can mark it as accepted answer thanks – Hien Nguyen Apr 30 '19 at 01:27
  • hmm so there is said "The input parameter is the number of words returned in the array after finishing the function.". so I guess it shouldn't work on every parameter of an array but use array.length? but I don't see exactly where you use new array as an parameter? –  Apr 30 '19 at 14:36
  • I add function fibwitharray you can check – Hien Nguyen Apr 30 '19 at 14:39
  • I updated answer with exact your requirement :) – Hien Nguyen Apr 30 '19 at 14:55