+ 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 Respuestas
+ 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