Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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]
 
PREVIOUS NEXT
Tagged: #fibonacci #sequence #javascript #loop
ADD COMMENT
Topic
Name
1+5 =