Closed jshreyans closed 5 years ago
You used less than or equal to here for (let i = 0; i <= inputs.length; i++) {
therefore it was accessing element out of the array index ( which returns undefined
) causing error to be thrown when you tried to access its addEventListener
. Also, the function does not implement the logic for previousCheck
previousCheck = this.id;. On correcting these two, it seems to work fine.
PS1: I suggest you ditch accessing elements using an explicit iterative for loop for more cleaner looking array looping techniques. forEach
would work here. (I guess since inputs
is a DOMTokenList
we can't call forEach
directly and need to convert it to array using Array.from
-> Array.from(intputs).forEach(function(elem, index) { // Do Computation})
although I am not completely sure if it is necessary or not.) In any case do have a look at these methods forEach
, map
, reduce
, some
, every
, find
.
Thanks a lot for the help and I'll try out the forEach
method too.
@LakshSingla
I went through the various Array methods you mentioned. However, as far as cleaner looking iteration techniques are concerned, I felt the for-of
method was the best. It makes the code much more readable.
Completed B3 PS: Need Help - I don't know why but the commented out part was not working even though it follows the same logic. So I had to use the inline function definition. Can't find the bug.