Answers for "fibonacci for loop javascript"

2

fibonacci sequence in javascript using for loop

let fibonacci = [0, 1]; // set initial conditions

for (let i = 2; i < 10; ++i) {
    // add the two previous elements to get the current element
    fibonacci[i] = fibonacci[i - 2] + fibonacci[i - 1];
}

console.log(fibonacci); // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Posted by: Guest on May-31-2021
1

js fibonacci sequence

var i;
    var fib = []; // Initialize array!

    fib[0] = 0;
    fib[1] = 1;
    for (i = 2; i <= 10; i++) {
      // Next fibonacci number = previous + one before previous
      // Translated to JavaScript:
      fib[i] = fib[i - 2] + fib[i - 1];
      console.log(fib[i]);
    }
Posted by: Guest on July-03-2020
0

javascript fibonacci example

// number fibonnaci to array format
function fibonacci(nums) {
  
  let fib = [0, 1];
  let data = [];
  
  for(let i = 2; i <= nums; i++) {
    fib[i] = fib[i - 1] + fib[i - 2]; 
    data.push(fib[i]);
  }
  
  return data;
}
Posted by: Guest on June-16-2020

Code answers related to "fibonacci for loop javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language