Тёмный
No video :(

JavaScript Algorithms - 7 - Fibonacci Sequence 

Codevolution
Подписаться 658 тыс.
Просмотров 91 тыс.
50% 1

⚡️ Code with me on Replit - join.replit.com...
⚡️ View and edit the source code on Replit - bit.ly/3PiRR7D
📘 Courses - learn.codevolu...
💖 Support UPI - support.codevo...
💖 Support Paypal - www.paypal.me/...
💾 Github - github.com/gop...
📱 Follow Codevolution
+ Twitter - / codevolutionweb
+ Facebook - / codevolutionweb
📫 Business - codevolution.business@gmail.com
Fibonacci Sequence
JavaScript Algorithms
Algorithms with JavaScript

Опубликовано:

 

24 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 86   
@ajiteshmishra
@ajiteshmishra 2 года назад
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
@ogucharles2800
@ogucharles2800 2 года назад
Hey pal, please is it possible for you to send how you traced it out steps on paper? I’m finding it difficult to do the same 😭😭😢
@bzchii7474
@bzchii7474 Год назад
@@ogucharles2800 u stilll need help brother?
@sam.antonielli
@sam.antonielli Год назад
@@bzchii7474 I do :((((
@bzchii7474
@bzchii7474 Год назад
@@sam.antonielli ask away
@sam.antonielli
@sam.antonielli Год назад
@@bzchii7474 I'm a rising junior right now in college, transferring from mechanical engineering into cs, im learning JavaScript right now on my own that way I have a decent understanding of algs and data structures and just programming in general before the fall, but it is sooooo confusing and I feel absolutely lost, is that normal? should I just keep pushing and any advice on learning?
@NadirAli-kn7sl
@NadirAli-kn7sl Год назад
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 }
@fakharuddin1923
@fakharuddin1923 2 месяца назад
This is what I was thinking as well
@toluajise4268
@toluajise4268 4 месяца назад
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
@bhagyawijenayake
@bhagyawijenayake 3 месяца назад
I believe he wants it so that if I enter "2", it indicates two numbers in the array; if I enter "7", it signifies seven Fibonacci numbers in the array. However, if I enter "7", it doesn't refer to the 7th Fibonacci number; instead, it indicates that there should be seven Fibonacci numbers in the array.
@roebucksruin
@roebucksruin 2 месяца назад
You're missing the 0th index. 0-6 is a length of 7. The syntax in the video is correct.
@ankushladani496
@ankushladani496 2 года назад
/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.
@ekaterinasu2914
@ekaterinasu2914 2 года назад
I have also implemented it this way! :))
@freakOuT..
@freakOuT.. 7 месяцев назад
did the exact same thing😂.
@urmil22
@urmil22 2 года назад
understood the concept in just 4 mins, very good and easy explanation
@akay995
@akay995 2 месяца назад
function fibonacci(n){ const fib=[0,1]; for(let i=2; i
@ROHAN4953
@ROHAN4953 Год назад
Thank you! Exactly what I needed. A short and simple explanation to revise algos. Subscribed!!
@khairiyusoff5040
@khairiyusoff5040 Год назад
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]
@emilylotz6589
@emilylotz6589 Год назад
thank you for this. This makes a tad bit more sense, and easier to follow.
@pillo1934
@pillo1934 Год назад
Thanks for this videos, i will watch all the course. Congrats for your work!
@kubataiupov1245
@kubataiupov1245 6 месяцев назад
Thanks for these series of videos!
@emmanuelmantilla1465
@emmanuelmantilla1465 9 месяцев назад
```const fibo = function (n) { const arr = [0, 1]; for (let i = 2; i < n; i++) { arr.push(arr[i - 1] + arr[i - 2]); } return arr; };```
@maahimaarrahahai
@maahimaarrahahai 2 года назад
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 ];
@mstalcup
@mstalcup 2 года назад
To fix this, line 6 could be: return fib.slice(0, n);
@ishantkushwaha534
@ishantkushwaha534 2 года назад
@@mstalcup It will work but it is not a good solution as slice will have its own time complexity of O(n) which would not be good for large input size. However, adding ' If condition ' would be a good choice in my opinion.
@falcon04v8
@falcon04v8 Год назад
here it works fine and it's not affecting time complexity function fibonacci(n){ const fib = [0, 1]; if(n == 0){ return null; } else if(n == 1){ return 0; } else { for(let i = 2; i < n; i++){ fib[i] = fib[i - 1] + fib[i - 2]; } return fib; } }
@spreadingtheknowledgeoflight
@spreadingtheknowledgeoflight 5 месяцев назад
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));
@Jai-xq5hi
@Jai-xq5hi 9 месяцев назад
Very simple explanation
@kjagan9813
@kjagan9813 2 года назад
Sir please do a videos on data structures as well .
@kaissarmouelhi9020
@kaissarmouelhi9020 Год назад
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.
@akaris8042
@akaris8042 2 года назад
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
@DriveandThrive
@DriveandThrive Год назад
It actually doesn't make more sense. It's slower. It works and is a good example of recursion but it makes the opposite of more sense. Your recursive method is O(2^n) while the for loop is O(n). Just because the code uses fewer characters doesn't make it 'make more sense'. IMO, faster is always better when writing functions and methods not matter how many lines the code is.
@laridot1942
@laridot1942 Год назад
How can we get the sequence with recursion, not just the nth value???
@user-pz8dg6ii7l
@user-pz8dg6ii7l 9 месяцев назад
​@@DriveandThriveHow is it recursion it executes once? It is a constant O(1)
@jahnavitaskani5461
@jahnavitaskani5461 4 месяца назад
thanks for these videos. These are so helpful
@Singh54321
@Singh54321 5 месяцев назад
function fibbonaci(n){ const fibarr=[0,1]; // console.log(fibarr.length); for (let i = fibarr.length; i
@chimsbeatzzedstar
@chimsbeatzzedstar 11 месяцев назад
You jave just earned a follower ❤
@suyashdeshpande784
@suyashdeshpande784 Год назад
function fibonacci(n){ let arr=[]; if(n==0){ arr.push(0); } else{ let a=0; let b=1; arr.push(a,b); for(let i=2;i
@sskhekhaliya
@sskhekhaliya Год назад
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.
@hectormerla
@hectormerla 10 месяцев назад
I don't know if you still need the answer but I'll give one so it can help others. In Javascript, Arrays are also Objects with properties inside, and "length" is one of them, Arrays keep track of the length so accessing the length is an O(1) operation not an O(n) (i.e. it's not actually counting all items to get the length every time you do "arr.length"). So to answer your question, using a "while" loop is an O(n) operation and everything inside are O(1) operations, so the result is still O(n) or linear.
@user-es2jt6qe2o
@user-es2jt6qe2o 6 месяцев назад
// i made it with recursion sir 😊 function fibo(n) { if (n === 0) return [0]; if (n === 1) return [0, 1]; const prevFibo = fibo(n - 1); return [...prevFibo, prevFibo[n - 1] + prevFibo[n - 2]]; } console.log(fibo(10));
@souvikkundu
@souvikkundu Год назад
Thank you
@shujaatali8414
@shujaatali8414 6 месяцев назад
Thank you I have done my code using push method. and O(n) time complexity is linear.
@chesseveryday9206
@chesseveryday9206 11 месяцев назад
Let array = [ ] function fibonacci(num) { Let a = 1 Let b = - 1 for (let i = 0 ; i < num; i++) { a = a + b b = a - b array.push(a) } return array } console.log(fibonacci(7)) it toke me 2h i'm still a beginner
@sangeethamani686
@sangeethamani686 Год назад
Thanks a lot for this video.
@aayushadhikari226
@aayushadhikari226 Год назад
const func = (num)=>{ let a = 0; let b = 1; let arr = [] arr.push(a) for (let i = 1; i < num; i++){ arr.push(b) let temp = a + b; a = b; b = temp } console.log(arr) } let num = 10 func(num);; //my way of solving this by pushing and swapping it...........
@rohitbagadi3058
@rohitbagadi3058 2 года назад
You declared fib as a const then how does the value of fib change after loop iteration???
@khairiyusoff5040
@khairiyusoff5040 Год назад
Actually fib is declared as an array which in turns is an object. You can change the value inside array nothing wrong with that. But you cant do that for primitives values like number, string, boolean.
@rohitbagadi3058
@rohitbagadi3058 Год назад
@@khairiyusoff5040 Okay, Thanks 🙂
@DriveandThrive
@DriveandThrive Год назад
Yes we wont be changing the array to say a string or number...so const makes sense fib wont be changing. Only the values in the array will change which is a different matter. If you try to assign fib = 3; or something const will kick in to protect it. If const somehow prevented arrays from changing it wouldn't be good.
@rohitbagadi3058
@rohitbagadi3058 Год назад
@@DriveandThrive 🙌🙂
@axljoven984
@axljoven984 29 дней назад
Here's my solution but I'm not sure if this is valid. function fibonacci(n) { let fib = [0,1]; while(fib.length !== n) { fib.push(fib[fib.length - 1] + fib[fib.length - 2]); } return fib; }
@Lolwithforrest
@Lolwithforrest Год назад
Do you recommend using semicolons or omitting them? Thank you for your work. I especially enjoyed your SvelteKit series.
@piyushaggarwal5207
@piyushaggarwal5207 Год назад
Question: What is the Big-O notation of [...Array(n)]? I used something else in my solution - [...Array(n)].forEach(() => { ... }) I want to know if the first part is a constant 1 or linear n. If the first part is linear n, will the whole Big-O become quadratic n^2?
@premsingh6967
@premsingh6967 Год назад
@rushikeshchougule5336
@rushikeshchougule5336 2 года назад
I think as loop runs 2times lesser than n the bigO will be O(n-2)
@paulbird2772
@paulbird2772 2 года назад
You are correct, however the big picture is that the -2 means very little with a larger input, and therefore it is normal to discard any constant values. If you look at the code there are several operations that are run n (-2) times you have the comparison of i < n you have the increment of i, you have the assignment of the array at index i you also have the addition. So you could also say O(4(n-2)). Again this multiple is dropped as the point of the notation is to compare constant / linear / quadratic / exponential functions in general terms.
@lukazashovski
@lukazashovski 7 месяцев назад
good, but using recursion is better :)
@lasith123
@lasith123 Год назад
// This also work fine. function fibonacci(n) { if (n
@user-es2jt6qe2o
@user-es2jt6qe2o 6 месяцев назад
This doesn't return array
@dhavalvanjara572
@dhavalvanjara572 2 года назад
Hi Vishawas please do series on redux saga with or without RTK after this. No good content for this on RU-vid so far.
@hectorserrano9314
@hectorserrano9314 2 года назад
Very true.
@tronganhnguyenthanh1157
@tronganhnguyenthanh1157 Год назад
In for loop, you input i = 2 , is it because of the numbers that you input in an array has two elements?
@KiraRu-pf6qq
@KiraRu-pf6qq 9 месяцев назад
i is an index of the elements in the array. The element's index in an array counts from 0 , so 0,1,2,3,4...etc. We already have const fib= [0, 1] (this is equal to i=1), so we are starting with i=2 and looking for elements until the index is equal to n. Hope that helps!
@hassanstudio7471
@hassanstudio7471 2 года назад
upload soon I'm waiting for you
@romimaximus
@romimaximus Год назад
Ok, good tutorial, ...but in our real world, ...what do you use this for ? ...and thankx for the great tutorial..😊👍
@skcreations2181
@skcreations2181 2 месяца назад
How the const array get modified 🤔
@mayurapriyianerodekailasam
@mayurapriyianerodekailasam 15 дней назад
const only works for primitive data types and not for non primitive data types like arrays
@Noobrado
@Noobrado Год назад
let A = [0, 1] for (let i = 0 ; i
@jericmoreno7088
@jericmoreno7088 9 месяцев назад
I tried it first on my own but man... hope I get more cleaner code like you and the other guys in the comments section 😅 const fibonacci= (n) => { let arr = []; let total =0 for(let i = 0; i < n; i++){ if(arr.length < 2){ arr.push(i) } else{ total = arr[i - 1] + arr[i - 2] arr.push(total) } } return arr } console.log(fibonacci(7))
@Unknownperson-ob7fu
@Unknownperson-ob7fu Год назад
I don't understand sir, what to do sir please help I am from commerce background, specially I dont understand the part where you implement fibonacci in javascript
@austinimbastari2760
@austinimbastari2760 Год назад
this confusing
@ujjawalsharma8363
@ujjawalsharma8363 2 года назад
Why are we declaring array as const ? I thought const is immutable.
@eliasgarcia1146
@eliasgarcia1146 2 года назад
it is immutable only if the entire array is declared again. it is allowed to add or remove values. the same happens with objects
@khairiyusoff5040
@khairiyusoff5040 Год назад
You can do this to an array: const arr = [1,2]; arr.push(3); But not this: const arr=[1,2]; arr=[1,2,3];
@kamal1002
@kamal1002 Год назад
const implies memory creation for the respective object is constant . so arrays and objects are mutable or we can change the internal values but the reference to the memory is same.
@allisonposey1946
@allisonposey1946 Год назад
I really want to buy your course but do you have a sale or discount?
@garepallijagadish3218
@garepallijagadish3218 2 года назад
This will not work for fibanocci(1)
@lvrsvid
@lvrsvid 2 года назад
Why wouldn't this be done with .push()
@oscargm1979
@oscargm1979 Год назад
It is the same,remember that accesing arr[index] is O(1) constant same as arr.push.Both ways have same time complexity.Choose what you want
@lvrsvid
@lvrsvid Год назад
@@oscargm1979 right on, thanks!
@isaacjon
@isaacjon Год назад
I don't understand why 0+1 equals to 2?and 1+1equals to 3???🤨🤨
@isaacjon
@isaacjon Год назад
@@allisonposey1946 thank you alisson
@FaisalAmin001
@FaisalAmin001 Год назад
@@allisonposey1946 The fibonacci number is the sum of last two numbers in array. Hope it help
Далее
JavaScript Algorithms - 8 - Factorial of a Number
4:42
Stepping Through Recursive Fibonacci Function
8:04
Просмотров 201 тыс.
DSPy Explained!
54:16
Просмотров 57 тыс.
What Is Recursion - In Depth
13:25
Просмотров 153 тыс.
JavaScript Loops Made Easy
10:52
Просмотров 168 тыс.