+ 1

ForEach

What is wrong with my forEach method? https://sololearn.com/compiler-playground/Wq0FGYNEjB2k/?ref=app Thanks

2nd Jul 2024, 6:08 PM
Zvi
Zvi - avatar
3 odpowiedzi
+ 2
"divs" is an object, not an array. you can try this: const divs=Array.from(document.getElementsByTagName("div")); if you want to make sure that it runs on sololearn playground, you need to wrap all your JS in window.onload = () => { ... } so that it only grabs rhe html elements when the body is fully loaded.
2nd Jul 2024, 6:42 PM
Lisa
Lisa - avatar
+ 1
So you mean that a regular for loop will work on objects but not a forEach?
2nd Jul 2024, 7:29 PM
Zvi
Zvi - avatar
+ 1
forEach an array method. common objects don't have a forEach method
2nd Jul 2024, 7:42 PM
Lisa
Lisa - avatar