Тёмный

Fibonacci Sequence Recursion Explained (JavaScript) 

Code the Things
Подписаться 491
Просмотров 20 тыс.
50% 1

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

 

15 окт 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 59   
@amitbhosale2633
@amitbhosale2633 Год назад
thankyou for explaining this code in such a simple and beautiful way I really loved it your channel is worth subscribing
@matthewc.9437
@matthewc.9437 Год назад
10/10 explanation. Been struggling to put these pieces together myself for hours and your video made it crystal clear. Thanks so much!!!
@bubbyprime
@bubbyprime 2 года назад
Now I see why memoization is helpful. @11:06 where you said were not remembering the value of fib(3). I didn't realize that this function had to call fib(3) again. Thank you for walking through this. I know this is about explaining mainly recursion, but I've been going through different videos trying to understand how the memoization works and you just made it click.
@codethethings4904
@codethethings4904 2 года назад
That's great! It's funny because this is what made memoization click for me as well.
@dolicious
@dolicious 3 года назад
this was such a good explanation !! when I was calculating my fib function using math I was not understanding how the correct number in the sequence was getting outputted but I forgot about calculating the base case ! it was very helpful going through line by line and each call stack. I admit I had to watch 5 times before I really got it but a great video and great explanation!
@internetdrew
@internetdrew 2 года назад
This was THE video that helped me understand this and taught me high to climb through it. The only thing I see here that is an issue is that the fib sequence actually starts at 0, so you'll want your "prev" variable to be 0, not 1, to be truly accurate. But if you just want to understand the logic in as plain English as possible, this is IT!
@codethethings4904
@codethethings4904 2 года назад
Glad to hear that! Yeah, haha, good catch. Someone pointed that out somewhere in the comments -- whoops :)
@robbiemclaughlin5572
@robbiemclaughlin5572 Год назад
Video was awesome, was having a hard time understanding the process between the input and output of a recursive function, this cleared it up, thank you for the help! Also what color theme are you using here?
@codethethings4904
@codethethings4904 Год назад
I can’t recall, sorry!
@bobthehuntsman1548
@bobthehuntsman1548 3 года назад
Thank you for the great explanation! Very clear and easy to comprehend.
@joyannejoy
@joyannejoy 2 года назад
Amazing walkthrough of what's going on in the call stack! That really clarified things for me!
@TheToxicPepsi
@TheToxicPepsi 4 года назад
This is a great video, thank you for making it !
@ishanillangakoon178
@ishanillangakoon178 Год назад
Thank U for the clear explanation 👍
@nauilnil4074
@nauilnil4074 Год назад
Thank you so much for such a detailed explanation!
@shahriarahmedraktim8255
@shahriarahmedraktim8255 3 года назад
this video has finished my all confusions....... thank you dear
@criaturaimaginaria5230
@criaturaimaginaria5230 3 года назад
hey you... you´re my hero, this is the best explanation of the fibonacci sequence I´ve ever seen. Very beautifully detailed and clear. thanks!
@KwabenaAgyemanLipsy
@KwabenaAgyemanLipsy 2 года назад
Really insightful explanation. concise and straight to the point. Thank you!!!
@khushwantdafre3230
@khushwantdafre3230 2 года назад
great explanation. Thank you
@curvesandcastles
@curvesandcastles 2 года назад
Great explanation!
@trim_3285
@trim_3285 Год назад
Thanks for the help man!
@tidumarco
@tidumarco 2 года назад
Thanks a lot for your great explanation! How did you configured your launch.json to automatically show the call stack and variables values? I can't find a way to do it. Thanks!
@codethethings4904
@codethethings4904 2 года назад
Check out this guide! code.visualstudio.com/docs/editor/debugging
@mahfujhossain5303
@mahfujhossain5303 3 года назад
A very elaborated explanation.Thank you!
@kir10s
@kir10s 4 года назад
Fantastic video! Thank you! Clearly demonstrates a confusing topic. Do you have a video showing how you got the dubugger to run correctly in VSC?
@codethethings4904
@codethethings4904 4 года назад
Thanks, I'm glad you found it helpful! I don't have a video about the VSC debugger, but what seems to be the issue? There is a good chance it has to do with how the "launch.json" file is configured in the .vscode folder. If I want to debug a single file, "binary_search.js" e.g., it might include a configurations property like this: "configurations": [ { "type": "node", "request": "launch", "name": "Launch Program", "program": "${workspaceFolder}/binary_search.js" } ] There is some good info here: code.visualstudio.com/docs/editor/debugging
@AddictedToCode387
@AddictedToCode387 2 года назад
function fib(n) { let fibList = []; if (n < 2) { fibList.push(0); } else { let prev = 0; let curr = 1; fibList.push(prev, curr); for (let i = 2; i < n; i++) { const next = prev + curr; prev = curr; curr = next; fibList.push(curr); } } console.log(fibList); }
@itouchgrass869
@itouchgrass869 2 года назад
great👍
@poweroftechnology7132
@poweroftechnology7132 Год назад
Thanks!
@PG1131
@PG1131 3 года назад
Great video. Very clean explanation. I like that you first illustrated the non-efficient method. It does make the more efficient that much easier to understand 👍 If you don't mind me asking, what do you use to run the call stack on the left side of vscode?
@codethethings4904
@codethethings4904 3 года назад
Thanks Patrick! That call stack is driven by the debugger extension built-in to VS Code, which is based on Node.js. You can read more about it here code.visualstudio.com/api/extension-guides/debugger-extension
@areegato
@areegato Год назад
This is the best explanation i've ever seen. Protect this man at all cost!
@adamkalimi3542
@adamkalimi3542 3 года назад
Very clear explanation, thank you.
@abhi_borkakoty
@abhi_borkakoty 3 года назад
Very nice explanation..loved it
@nChauhan91
@nChauhan91 3 года назад
Awesome explanation, subscribed :)
@froylanrodriguez7624
@froylanrodriguez7624 2 года назад
Hey I am a bit confused, you said we do not remember the value of fib(3) when trying to calculate fib(5) since it was popped off the stack, which made 100% sense @11:06; however, earlier when calculating fib(4) you said "we just returned the value of fib(3) so we know it" but how is this possible if it was popped off the stack?? @9:20 is it because it was the previous calculation of fib(4)? sorry for the weird question, loved the video tho man, great stuff!
@codethethings4904
@codethethings4904 2 года назад
"is it because it was the previous calculation of fib(4)? " Yep, that's exactly right! Since fib(4) is defined as fib(3) + fib(2) we first calculate fib(3), and once we know that value, popping it off the stack basically means that we use that value to replace the call to fib(3) in the expression to calculate fib(4), so that now we have fib(4) = 2 + fib(2)
@froylanrodriguez7624
@froylanrodriguez7624 2 года назад
@@codethethings4904 thank you so much man, ok now I feel like a KING haha felt like a noob asking that, awesome it all makes sense now. Have a good rest of your weekend!
@itouchgrass869
@itouchgrass869 2 года назад
@@codethethings4904 Thanks, now that makes sense for me too
@anaselmakhloufi2180
@anaselmakhloufi2180 Год назад
thanks bro amazing
@calvinbarajas5144
@calvinbarajas5144 2 года назад
This was amazing, thank you.
@mydaily21
@mydaily21 2 года назад
halo sir, how if to find nearest the fibonacci? ex: if input [15,1,3], then my program expected to made output = 2 Because by the above input example **[15,1,3]**, if we add up them it will **15+1+3 = 19**, and the nearest **fibonacci of 19 is 21**, so we need to **add 2 then it can be 21**.
@codethethings4904
@codethethings4904 2 года назад
You could do this in a straightforward way by just generating the sequence yourself and measuring the distance between your number (19 in the example) and two numbers in the sequence. As long as the distance to the next fib number is less than the previous, you can keep generating the sequence. This would be O(n). If you use an existing list of the sequence, you could do a binary search to get the left and right values of the sequence and get this down to O(log(n)). Here is an example of the first approach: function nearestFib(num) { let prevFib = 0 let currentFib = 1 let nextFib while (Math.abs(num - currentFib) < Math.abs(num - prevFib) || prevFib === currentFib) { nextFib = prevFib + currentFib prevFib = currentFib currentFib = nextFib } return prevFib }
@mydaily21
@mydaily21 2 года назад
@@codethethings4904 thanks for sharing
@mmmmmmmmmatt
@mmmmmmmmmatt Год назад
Does Anybody know the time and space complexity? I think its O(1) for both space and recursive time and o(n) for time on the for loop
@geld5220
@geld5220 2 года назад
wow - finally.. thank you.
@geld5220
@geld5220 2 года назад
every second of this video made sense to me. I cannot thank you enough. Even that part where you were on the last call stack where fib(3) had to be recursed again. The way you stacked up the calls, everything here make sense. I finally got it. Thank you!
@tatjanaburdett2854
@tatjanaburdett2854 3 года назад
Great explanation, that helps! :D
@theblackalbino1994
@theblackalbino1994 2 года назад
Why does this not work when you return next instead of curr? curr = next so you should get the same value right?
@codethethings4904
@codethethings4904 2 года назад
In the iterative approach, next is scoped to the for loop, so you wouldn’t be able to return next outside of it, and if you want to return within it, you have to change the condition so that it runs when
@byevrolex
@byevrolex 2 года назад
why is i set to 2 and not 0 in the while loop?
@codethethings4904
@codethethings4904 2 года назад
Because we start at the 2nd number in the sequence (although, as someone pointed the sequence actually starts at 0, so it would probably be better to start with prev at 0, curr at 1, and i at 1)
@ArtClayHobby
@ArtClayHobby 11 месяцев назад
What is the meaning and use of "return"?
@kirkrichardson7541
@kirkrichardson7541 11 месяцев назад
This could have a long and complex answer, but generally in programming languages, return is a keyword used to indicate the result of a function call. In this case what we produce is another function call, and another, and another until finally we return a value, which then gets referenced in the previously returned function calls until eventually we've called all the functions we've deferred and return a final value.
@dadmomnviolynt
@dadmomnviolynt 4 года назад
what theme do you use for that pastel color?
@kithowlett8374
@kithowlett8374 3 года назад
I think the theme is called Dracula. draculatheme.com/visual-studio-code
@Shabztubeyou
@Shabztubeyou 3 года назад
the fibonnaci sequence starts at 0 though :s
@codethethings4904
@codethethings4904 3 года назад
Whoops 🙃
@BusinessWolf1
@BusinessWolf1 2 года назад
Don't treat the audience like complete idiots
@sowmocoding5740
@sowmocoding5740 3 года назад
Great explanation!
Далее
What is Recursion | Javascript | Real World Examples
26:35
Stepping Through Recursive Fibonacci Function
8:04
Просмотров 206 тыс.
All Rust string types explained
22:13
Просмотров 176 тыс.
5 Simple Steps for Solving Any Recursive Problem
21:03
JavaScript Memoization Made Simple!
11:22
Просмотров 6 тыс.
Just enough C to have fun
39:29
Просмотров 53 тыс.