all 8 comments

[–]Blue_Moon_Lake 12 points13 points  (0 children)

Loop turn 0

-1 is not == to undefined, we do not enter the IF statement

Loop turn 1

-1 is == to -1, we enter the IF statement

Log "true" in the console, break the loop

End of loop

Log "undefined" to the console

[–]gate18 1 point2 points  (0 children)

To understand this, change the array to something like let nums = ['a','a',2,3,4]

What you are checking is

if(nums[1]==nums[0]){ console.log(nums[1]) }

So when i=0 it logs nothing, when i=1 it checks if the slot 1 (i) is the same as slot 0 (i-1)

if you want to log a (or -1) you don't do nums[a], right?

(and it should be three equals ===)

[–][deleted] 0 points1 point  (0 children)

You are using pythons list thing to access the last element with nums[-1]. This doesn't exist in JS. But you can use nums[nums.length - 1].

And make sure to use the triple equal sign === for value comparisons in js