Seeing var _LP = Date.now(); when using the debugger?

Seeing var _LP = Date.now(); when using the debugger?
0

#1

I am working through the Javascript Intermediate Algorithm Scripting problems, and keep seeing this occur when I use certain loops. I don’t ever write this into my code, but when I add in debugger to figure out why my code won’t work, I see a var _LP = Date.now(); added in right before the beginning of the loop, followed by a if (Date.now() - _LP > 100) break; right after it. I can’t tell if it’s the code I wrote that exits out of these loops early, or this random var that’s causing it to behave that way. Am I missing something?

See screenshot below for example. Here’s the link to the problem: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers

Thanks!
Intermediate%20Algorithm%20Scripting%20Sum%20All%20Odd%20Fibonacci%20Numbers%20%20Learn%20freeCodeCamp%202018-07-22%2019-01-43


#2

Debugger has its place, but I think you’d be better off using console.log() to figure out logic issues.

That being said, since you are concerned about a loop exiting early, the problem then lies on whatever conditions are allowing the while loop to continue/discontinue. In this case, something about curr and num causes you to leave the loop early.

Another plausible problem I see is that the first 2 numbers of a fibo sequence is 1 and 1. I don’t think your solution takes that into account. As it is right now, it is lost in your algorithm, and I think that is because

prev = curr;
curr = curr + prev;

should be looked at carefully. Think what you are doing in those lines of code and if it’s what you want to do.

Can’t really help you any further without seeing the rest of your code, and perhaps copy pasting the code it to see the results.

Also, please copy the link to the challenge or use the help button


#3

I assume LP == Loop Protection. Anyway, it’s injecting a timer into your code to allow tests to bail out (the loop will just exit if it hits that break statement) if your code takes too long to run/gets stuck in a loop. It’s for the tests: if the code takes more than 100ms to get where it should, execution is halted


#4

Thanks all! Just wanted to double check that it was my code that was wrong, and not this break statement that was causing my code to break from the loop. Also, good to know that it’s breaking based on ms of time spent in the loop.


#5

I’ll have to retract my debugger statement. I just prefer console.log because I can see everything, but when I look at this case, I’m completely wrong about the usefulness of debugger.

So keep using debugger, and ignore what I said about it.


#6

Haha, yeah, the debugger can be incredibly powerful for problems like these, which is why it’s frustrating that it exits from the loop so early! Not really sure why that var has been added in. Oh well.


#7

Anyway, I figured out what was wrong with my code. I was assigning the new value of prev to my prev variable, rather than the actual previous value. I needed to change it to something like

newCurr = prev + curr;
prev = curr;
curr = newCurr;

and you were right about taking into account the first two 1s.

Thanks for the help!


#8

No problem. Good luck!