Untitled

 avatar
unknown
plain_text
a year ago
1.4 kB
6
Indexable
const arr = [1,2,3];
let num =  Math.floor(Math.random()*arr.length);

num=arr[num];
console.log(num)

let userInput;

const userImg = document.querySelector("#userImg");
const compImg = document.querySelector("#compImg");
const choiceImg = document.querySelectorAll(".choiceImg");
const result = document.querySelector("#result");

choiceImg.forEach(button => {
    button.addEventListener("click", function(){
        console.log(button.id)
        //storing user's input
        userInput= Number(button.id);

        //displaying user selected Input
        userImg.src = `img/${button.id}.png`

        //displaying computers image
        setTimeout(function(){
            if(userInput == num){
                result.textContent = "Draw"
            } else if(userInput === 2 && num == 1 || userInput == 3 && num == 2 || userInput == 1 && num == 3)
            {
                result.textContent = "You Win"
            }else{
                 result.textContent = "Computer Wins"
            }

            // displaying computers image
            compImg.src = `img/${num}.png`}, 1000)
        num =  Math.floor(Math.random()*arr.length + 1);
        result.textContent = '';
        

       
        

    })
    /*
    stone: 1
    paper: 2
    scissor:3

    1 ,2 = 2 
    2, 3 = 3
    1, 3 = 1
    */
   
   
});


Editor is loading...
Leave a Comment