OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

I trying to create a variable and assign a value to it using img but bf the variable is assigned the alert at the end of the code is already executed

  • Thread starter Thread starter Lucas dos santos silva
  • Start date Start date
L

Lucas dos santos silva

Guest
I trying to create a variable and assign a value to it using img but bf the variable is assigned the alert at the end of the code is already executed.

After the alert occurs, the function works normally, emitting an alert according to the selected image

github: https://github.com/LucasDSS888/Project-Rock-Paper-Scissors/tree/rps-ui

Code:
let humanChoiseNumb = humanChoiseImgClick();


function humanChoiseImgClick() {
    
    let num;

    const choiseAP = document.querySelector(".card.alexP");
    choiseAP.addEventListener("click", () => {
        num = 2;
        alert(num);
    });
    
    const choiseAR = document.querySelector(".card.alexR");
    choiseAR.addEventListener("click", () => {
        num = 1;
        alert(num);
    });

    const choiseAS = document.querySelector(".card.alexS");
    choiseAS.addEventListener("click", () => {
        num = 3;
        alert(num);
    });

    return num;

}

alert(humanChoiseNumb);

I expected the alert to be issued according to the value assigned to the humanChoiseNumb variable

<p>I trying to create a variable and assign a value to it using img but bf the variable is assigned the alert at the end of the code is already executed.</p>
<p>After the alert occurs, the function works normally, emitting an alert according to the selected image</p>
<p>github: <a href="https://github.com/LucasDSS888/Project-Rock-Paper-Scissors/tree/rps-ui" rel="nofollow noreferrer">https://github.com/LucasDSS888/Project-Rock-Paper-Scissors/tree/rps-ui</a></p>
<pre><code>let humanChoiseNumb = humanChoiseImgClick();


function humanChoiseImgClick() {

let num;

const choiseAP = document.querySelector(".card.alexP");
choiseAP.addEventListener("click", () => {
num = 2;
alert(num);
});

const choiseAR = document.querySelector(".card.alexR");
choiseAR.addEventListener("click", () => {
num = 1;
alert(num);
});

const choiseAS = document.querySelector(".card.alexS");
choiseAS.addEventListener("click", () => {
num = 3;
alert(num);
});

return num;

}

alert(humanChoiseNumb);
</code></pre>
<p>I expected the alert to be issued according to the value assigned to the humanChoiseNumb variable</p>
 
Top