samedi 15 août 2020

If else statement is not working after clicking button in the browser

I want to build a game using javascript, DOM, HTML, and CSS. My logic is as follows: Step 1: Clicking three buttons execute three different functions, named random1, random2, and random3.Each function will generate two random numbers. (Suppose 1 and 2).

Step2: Checking the result of these functions by using if/else-if conditions. Step3: If the return value of all the buttons equals 1 it will alert ("player 1 wins"). Similarly, if value== 2, 'player 2 wins'will be alerted.

Step4: If values do not equal to each other, the program will ask to refresh the browser.

But the problem is when I run this code in the browser only the else statement is working and if/else-if is not working. Another problem: else statements work before displaying the random number in the

tag when clicking 'button number three'.

//My Code//

var a;
function random1(){
    a = Math.floor(Math.random()*2+1);
    document.getElementById("para1").innerHTML = a;
    return a;       
}
let p1 = document.getElementById("myBtn1");
p1.addEventListener("click", random1);

var b
function random2(){
    b = Math.floor(Math.random()*2+1);
    document.getElementById("para2").innerHTML = b;
    return b;       
}

let t2 = document.getElementById("myBtn2")
t2.addEventListener("click",random2);

var c
function random3(){
    c=Math.floor(Math.random()*2+1);
    document.getElementById("para3").innerHTML = c; 
    return c;    
}

let r3 = document.getElementById("myBtn3");
r3.addEventListener("click",check(a,b,c));
r3.addEventListener("click",random3);


function check(s,t,u){
    if(s===1 && t===1 && u===1){
        alert("Players 1 Wins");
    }
    else if(s===2 && t===2 && u===2){
        alert("Player 2 Wins");
    }
    else{
        alert"Refresh the Browser");
    }
}

Aucun commentaire:

Enregistrer un commentaire