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.
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!
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!
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?
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!
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
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?
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
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!
"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)
@@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!
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**.
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 }
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!
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
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)
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.