+ 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

2nd Jun 2020, 7:48 AM
Ginfio
Ginfio - avatar
9 Respostas
+ 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....
3rd Jun 2020, 6:49 AM
Ginfio
Ginfio - avatar
+ 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.
3rd Jun 2020, 4:53 PM
Ginfio
Ginfio - avatar
+ 2
It may seem like the (else if) are necessary but they're not. https://code.sololearn.com/WpSH6TOG04mt/#js
3rd Jun 2020, 5:19 PM
ODLNT
ODLNT - avatar
+ 2
ODLNT O, I see. Make sense. Thanks a lot.
3rd Jun 2020, 5:31 PM
Ginfio
Ginfio - avatar
+ 1
Your code is working fine
2nd Jun 2020, 8:22 AM
Souptik Nath
Souptik Nath - avatar
+ 1
SOUPTIK NATH you don’t see any TypeErrors? Run the code, click “Previous” and see wut happenz
2nd Jun 2020, 8:26 AM
Ginfio
Ginfio - avatar
+ 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.
3rd Jun 2020, 4:07 AM
ODLNT
ODLNT - avatar
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)
3rd Jun 2020, 9:42 AM
ODLNT
ODLNT - avatar
- 1
i < elements.length
2nd Jun 2020, 8:22 AM
CalviŐČ
CalviŐČ - avatar