Languages
[Edit]
EN

JavaScript - Iterative Fibonacci sequence / number algorithm

13 points
Created by:
Root-ssh
68980

Using JavaScript it is possible to compute Fibonacci sequence in following way.

1. Iterative algorithm example

// ONLINE-RUNNER:browser;

function calculateValue(number) {
  	if(number < 1) {
    	return 0;
    }
  
    var a = 0;
    var b = 1;

    for (var i = 1; i < number; ++i) {
		var c = a + b;
      
      	a = b;
      	b = c;
    }

    return b;
}

// Example:

for (var n = 0; n < 10; ++n) {
    var value = calculateValue(n);

    console.log('f(' + n + ')=' + value);
}

 

Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join