I’m writing simple rock-paper-scissors game. In this piece of code I’m trying to assign id value of the clicked img to a variable using .forEach method. It just doesn’t work. If statements does work but somehow string doesn’t add to a variable.
const weapons = document.querySelectorAll("img");
let playerWeapon = "";
weapons.forEach((weapon) => {
weapon.addEventListener("click", (event) => {
if (weapon.id == "paper") {
playerWeapon += "paper";
} else if (weapon.id == "scissors") {
playerWeapon += "scissors";
} else if (weapon.id == "rock") {
playerWeapon += "rock";
}
});
});
console.log(playerWeapon);
I’ve replaced concatenation with console.log to see if it returns value and it does return them. Tried adding return before concatenation, didn’t help.
2
Answers
The problem is that you are immediately logging the value of
playerWeapon
, which means that the logged value will be""
since the user doesn’t have time to click on any of the images before the value is logged.Maybe something you could do is add an extra button that would log
playerWeapon
when clicked.You are facing the difference between
synchronous
andasynchronous
code.Javascript usually executes the code synchronously, which means the JS interpreter executes immediately one line at a time from top to bottom.
But you are using a
callback function
as well, which will be executed asynchron(at a later time) – when a user clicks on aweapon
.Lets have a closer look into your code:
The second argument of
addEventlistener
is a callback function. A callback function is a javascript function, which is passed as parameter into another function. You have to be aware that this function is just defined but not executed yet!This function will be executed internally.
So when you run your code, than your
console.log(playerWeapon)
will be executed before the code in your callback function. Thats the reason for getting just an empty string in your console output.What you can do instead of use
console.log
is adding an HTML Element which shows your clicked weapon or just useconsole.log
e.g