3 ms·
Slightly challenging one. Find the potential bug. const arr = [1, 2, 3, 4, 5] for (let i = arr.length - 1; 0 < i; i--) { const j = Math.floor(Math
by threatofrain 3y ago
Slightly challenging one. Find the potential bug.
const arr = [1, 2, 3, 4, 5]
for (let i = arr.length - 1; 0 < i; i--) {
const j = Math.floor(Math.random() * (i + 1))
[arr[i], arr[j]] = [arr[j], arr[i]]
}
- genezeta 3y agoNot potential: Uncaught ReferenceError: can't access lexical declaration 'j' before initialization Semicolons aren't that bad.
- XeO3 3y ago> In general, if a statement begins with (, [, /, +, or -, there is a chance that it could be interpreted as a continuation of the statement before, if semicolon isn`t provided. While /, +, and - are rare in the beginning of the statement, ( and [ aren't.
- WorldMaker 3y agoThat list is missing ` from ES2015+ template literals. I enjoy teaching this to junior developers as the "winky frown rule" because that is a silly name that sticks in your memory and does help you remember it. Lines that start with an emoticon frown must wink: ;( ;[ ;`