+ 1
JS code help
What Iâm about to say will make sense once you run the code. let elements = doc...selectorAll(âelemâ) // 13 of them. So in the if statement, when I put a number for max, it works perfectly fine: Example: if (i <= 13) { do.something } // works. That works fine. But if I do: if (i <= elements.length) { do.something } // does not work ... error Why might that be, and how can we fix it? https://code.sololearn.com/WVFKv0ICtAd8/?ref=app
9 RĂ©ponses
+ 2
ODLNT đ
Ok, after many tries, Iâm out of ideas.
I tried;
len = elem.length + 1; (and - 1).
// didnât work. good try though
And then, tried to add 1 to i.
var i = 1;
I tried the opposite:
i = 0 - 1 (-1)
Still no.
The first solution, at the top that looks like it would work, but....
+ 2
ODLNT That helps.
Itâs working like it should now.
But, I didnât get the â...too much (else if)â part. I tried to switch things around, and simplify to one âif (...) else {...}â statement, but that wonât work.
Like... all those else ifs are necessary.
+ 2
It may seem like the (else if) are necessary but they're not.
https://code.sololearn.com/WpSH6TOG04mt/#js
+ 2
ODLNT O, I see. Make sense.
Thanks a lot.
+ 1
Your code is working fine
+ 1
SOUPTIK NATH you donât see any TypeErrors?
Run the code, click âPreviousâ and see wut happenz
+ 1
The length of the element list is 13 but indexing starts at 0 and end at 12 in this case. You need to account for that.
0
Ginfio
The first solution looks like it would work because it does work. But your logic is verbose /overkill, you need to simplify.
Hint: too too much (else if)
- 1
i < elements.length