DEV Community

Discussion on: All The Ways That I Failed At "Sum 3"

Collapse
 
steveochoa profile image
Steve O • Edited

I loved your thorough explanation!

I did find an issue in your final code which you posted:

When you iterate over the array, you use this conditional here:
if (i !== j && num !== nums[i - 1])

This will instantly fail. On the first iteration, i=0, therefore nums[i-1] is immediately OOB.
You can use a proxy in JS to allow negative indices fwiw.
Also, i will never equal j.