“JavaScript mendapatkan nomor fibonacci” Kode Jawaban

Urutan Fibonacci dalam JavaScript

let fibo = [0, 1]
for (var i = 2; i <= 10; i++) {
    fibo[i] = fibo[i - 1] + fibo[i - 2];
}
console.log(fibo)
//Output: [ 0, 1,  1,  2,  3,5, 8, 13, 21, 34, 55]
Ariful Islam Adil(Code Lover)

JavaScript mendapatkan nomor fibonacci

/**
 * Method that calculates the Fibonacci Sequence and returns the n'th value of the sequence
 * @param {*} pos 
 */
const getFibPosition = (pos) => {
    if (pos === 0) return null;
    if (pos === 1) return 0;
    if (pos === 2) return 1;

    let fibArray = [0, 1];
    let aux1, aux2;

    for (let i = 2; i < pos; i++) {
        aux1 = fibArray[i - 1];
        aux2 = fibArray[i - 2];

        fibArray.push(aux1 + aux2);
    }

    return fibArray[pos - 1];
}
Zé Pedro Mota

Contoh Javascript Fibonacci

// 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;
}
Restu Wahyu Saputra

Nomor Fibonacci dalam JavaScript

function fibonacciRecursive(n) {
	if (n < 2) {
    	return n
    }
  return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2)
}

fibonacciRecursive(8);
// 21
Dr.Strange

Jawaban yang mirip dengan “JavaScript mendapatkan nomor fibonacci”

Pertanyaan yang mirip dengan “JavaScript mendapatkan nomor fibonacci”

Lebih banyak jawaban terkait untuk “JavaScript mendapatkan nomor fibonacci” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya