articleBody
| -
Hi @AvidLearner,
congratulations on sorting your problem out! Pretty much all of the
ways of return multiple element references will return an array
that you need to loop through (whether it is getElementsByTagName,
getElementsByName, or the more modern querySelectorAll).
I don’t tend to give you an exact grade, as I just don’t
have time to mark all the assessment entries in detail. What I can
do is test your code (it is a little different to mine, but it
works fine, well done), and give you links to the marking guide and
finished code so you can check it yourself:
Marking guide:
https://github.com/mdn/learning-area/blob/master/javascript/building-blocks/gallery/marking-guide.md
Live example:
https://mdn.github.io/learning-area/javascript/building-blocks/gallery/
Source code:
https://github.com/mdn/learning-area/blob/master/javascript/building-blocks/gallery/main.js
-
@chrisdavidmills I have some questions,
though, if you don’t mind me asking.
Here, my function sourceAlter only has the
parameter e and therefore, I did not pass any
parameters when I called the function, hence
image[i].addEventListener('click', sourceAlter)
without any parentheses and parameters. However, what if my
function requires two parameters, e.g. sourceAlter(a,
b), how do I pass on the parameters to said function?
I don’t quite understand how looping works in JavaScript. It
somehow does not make sense to me how for (...) in my
code above worked. I do know that it loops five times (as expressed
in the conditional statement i < image.length) and
that whenever an image with the order i
is clicked, we run a function. But, when we click picture 1, 2,3
and then 1 again, it somehow worked? There is not decrement in the
for loop, so how did it happen? Is the loop run over
again after each action (in this case, click (but doesn’t have to
be necessarily a click))?
|