dimanche 17 mai 2020

Rock, Paper, Scissors If Statement Not Working

The code is working until "if statement". I feel the logic is correct, but I guess the syntax is not. I hope the code is clear enough. I have three buttons in HTML for rock, paper and scissors and each button has "onclick" function. For example when the "rock" button is pressed, "rock" shows up in h3 id="yourChoice". There is another h3 id="computerChoice" which two I wanted to compare in if statements, but without intended outcome. Thank you!

var rock = document.getElementById("rock");
var paper = document.getElementById("paper");
var scissors = document.getElementById("scissors");

var computerChoice = document.getElementById("computer_choice");
var yourChoice = document.getElementById("your_choice");

var computerScore = document.getElementById("computer_score");
var yourScore = document.getElementById("your_score");

function random(mn, mx){
    return Math.random()*(mx - mn) + mn;
}
function userRock(){
    computerChoice.innerHTML = arr[Math.floor(random(1, 4))-1];
    yourChoice.innerHTML = "rock";
}
function userPaper(){
    computerChoice.innerHTML = arr[Math.floor(random(1, 4))-1];
    yourChoice.innerHTML = "paper";
}
function userScissors(){
    computerChoice.innerHTML = arr[Math.floor(random(1, 4))-1];
    yourChoice.innerHTML = "scissors"
}

//the following part is not working
if(yourChoice == "paper" && computerChoice == "paper"){
    alert("It`s a tie!");
}else if(yourChoice == "paper" && computerChoice == "scissors"){
    alert("You lose!");
}else if(yourChoice == "paper" && computerChoice == "rock"){
    alert("You win");
}else if(yourChoice == "rock" && computerChoice == "paper"){
    alert("You lose!");
}else if(yourChoice == "rock" && computerChoice == "rock"){
    alert("It`s a tie");
}else if(yourChoice == "rock" && computerChoice == "scissors"){
    alert("You win!")
}else if(yourChoice == "scissors" && computerChoice == "scissors"){
    alert("It`s a tie");
}else if(yourChoice == "scissors" && computerChoice == "rock"){
    alert("You lose!");
}else if(yourChoice == "scissors" && computerChoice == "paper"){
    alert("You win!")
};```





Aucun commentaire:

Enregistrer un commentaire