JavaScript Algorithms - 7 - Fibonacci Sequence

preview_player
Показать описание

📱 Follow Codevolution

Fibonacci Sequence
JavaScript Algorithms
Algorithms with JavaScript
Рекомендации по теме
Комментарии
Автор

I can't believe myself but I write it on paper in steps in bullet points and then write the code accordingly and it works wow it really boosts my confidence, feeling like a impostor for long time

ajiteshmishra
Автор

function fibonacci(n) {
let series = []
if (n === 0) {
series.push(0)
} else {
series.push(0, 1)
}
for (let i = 2; i < n; i++) {
series.push(series[i - 1] + series[i - 2])
}
return series
}

NadirAli-knsl
Автор

Thank you so much for this course. However I have a little observation for the loop condition. Take for example when n=7
This: for (let i = 2; i < n; i++) {
because your loop condition causes the loop to stop iterating before reaching the nth term. That't why we have
Should have been this I guess: for (let i = 2; i <= n; i++) {
This change ensures that the loop generates the Fibonacci sequence up to and including the nth term, as intended.
[
0, 1, 1, 2,
3, 5, 8
]
Since the Fibonacci sequence generated by the code starts from the 0th term and includes the 7th term. So, I think it should include a total of 8 terms.
[
0, 1, 1, 2,
3, 5, 8, 13
]
Please let me know if my observation is right.

toluajise
Автор

/function fibbonacci(n) {
let a = 0;
let b = 1;
const arr = [];
for (let i = 0; i < n; i++) {
let c = a + b;
arr.push(a);
b = a;
a = c;
}
return arr;
}
console.log(fibbonacci(10);

We can use this way too.
Thank You sir for this awesome series.

ankushladani
Автор

understood the concept in just 4 mins, very good and easy explanation

urmil
Автор

Thank you! Exactly what I needed.
A short and simple explanation to revise algos.
Subscribed!!

ROHAN
Автор

function fibonacci(n){
const fib=[0, 1];
for(let i=2; i<n; i++){
fib.push(fib[i-1] + fib[i-2]);
}
return fib;
}

This worked for me

akay
Автор

function fibonacci(n) {
let output = [0, 1];
for (let i = 0; i < n - 2; i++) {
output.push(output[i] + output[i + 1]);
}
return output;
}

console.log(fibonacci(7)); //[0, 1, 1, 2, 3, 5, 8]
console.log(fibonacci(5)); //[0, 1, 1, 2, 3]
console.log(fibonacci(10)); //[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

khairiyusoff
Автор

Would like to add that fibonacci(1) would return [ 0, 1 ] which is incorrect. We can use an if condition to return the array as [ 0 ];

maahimaarrahahai
Автор

Thanks for this videos, i will watch all the course. Congrats for your work!

pillo
Автор

I think (might be wrong . Take it with a pinch of salt) it's important to mention that the fib array is likely to grow in the future. It depends on the input size of n. Here memory also consumes as much as the array requires. Space complexity is of O(n), too. This fact puts the function through critical interrogations since both time & space are heavily dependent on the input size.

kaissarmouelhi
Автор

Sir please do a videos on data structures as well .

kjagan
Автор

thanks for these videos. These are so helpful

jahnavitaskani
Автор

Using Recursion makes more sense for Fibonacci Sequencing

function fib(n){
if(n ===1 || n ===2){
return 1
}
return fib(n-1)+ fib(n-2)
}
console.log(fib(12)) // 144
console.log(fib(10)) // 10
console.log(fib(11)) // 89

akaris
Автор

function fibbonacci(n) {
const fib = [0, 1];
var tmp = [];
if (n == 0) {
return tmp;
}
if (n == 1) {
tmp = tmp[0];
return fib;
}
for (let i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib;
}
console.log(fibbonacci(5));
console.log(fibbonacci(0));

spreadingtheknowledgeoflight
Автор

if we use while loop instead of for like:

while (arr.length < n){
arr.push(arr[arr.length-1] + arr[arr.length-2]);
}

then what will be Big-O time complexity, O(n^4)? because it is checking length every time four time and one time use push.

sskhekhaliya
Автор

Thank you I have done my code using push method. and O(n) time complexity is linear.

shujaatali
Автор

You declared fib as a const then how does the value of fib change after loop iteration???

rohitbagadi
Автор

function fibbonaci(n){
const fibarr=[0, 1];
// console.log(fibarr.length);
for (let i = fibarr.length; i <n; i++) {

};
console.log(fibarr);
}

fibbonaci(1000);

Singh
Автор

Do you recommend using semicolons or omitting them? Thank you for your work. I especially enjoyed your SvelteKit series.

Lolwithforrest
welcome to shbcf.ru