Rock, Paper, Scissors will not run the compare function


#1


Below is my code, when I load the page I get the initial alert asking me to choose rock, paper, or scissors, then nothing happens! The code should run the compare function then display a js alert with the outcome but for some reason it just does NOTHING. No errors or anything. I've been staring at this for hours and for the life of me can't find any issues with my code? Anyone see the issue here? Thanks!


<!DOCTYPE html>

<html>

<head>
<title>Rock, Paper, Scissors - pn</title>
<script>
    
var ELuser = prompt("Choose wisely... Rock, Paper, or Scissors?");    
var ELcomp = Math.random();

    if(ELcomp <=0.34) {
        ELcomp="rock";    
        
    } else if(ELcomp <=0.67){
        ELcomp="paper";
    } else {
        ELcomp="scissors";
    }
    
    //BEGIN COMPARE FUNCTION
    var compare = function(userChoice,compChoice){
        
        //TIE RESULT
        if (userChoice===compChoice){
            alert ("It is a tie!");
        }
        
        //USER SELECTS ROCK
        if(userChoice==="rock"){
            if(compChoice==="scissors"){
                alert ("You've won! Your Rock beats my Scissors!");
            }
            else {
                alert ("You've LOST!  My paper beats your rock!");
            }
        }
        
        //USER SELECTS PAPER
        if(userChoice==="paper") {
            if(compChoice==="rock"){
                alert("You've won! Paper beats my rock");
            }
            else {
                alert("You've LOST! My scissors beat your paper");
            }
        
            //USER SELECTS SCISSORS
            if(userChoice==="scissors"){
                if(compChoice==="rock"){
                    alert("You've LOST! My rock beats your scissors");
                }
                else {
                    alert ("You've WON! Scissors prevails!");
                }
            }
            
        
        }
        
    };
    
compare(ELuser,ELcomp);    

</script>    
</head>    
<body>
<p>Hello</p>    
    
</body>

</html>


#2

Hi man.

I've changed "compare" function adding "else if" inside, but I think the big problem was the question you chose "Rock, Paper or Scissors". And the function was comparing "Paper" with "paper", !== strings.

Take a look below, its working now::

var ELuser = prompt("Choose wisely... Rock, Paper, or Scissors?");    
console.log("User: " + ELuser);

var ELcomp = Math.random();

    if(ELcomp <=0.34) {
        ELcomp="rock";    
        
    } else if(ELcomp <=0.67){
        ELcomp="paper";
    } else {
        ELcomp="scissors";
    }
    console.log("computer: " + ELcomp);
    
    //BEGIN COMPARE FUNCTION

    var compare = function(userChoice, compChoice){
        
        //TIE RESULT
        if (userChoice === compChoice){
            console.log ("It is a tie!");
        
        
        //USER SELECTS ROCK
        } else if (userChoice === "Rock"){
            if (compChoice === "scissors"){
                return ("You've won! Your Rock beats my Scissors!");
            }
            else {
                return ("You've LOST!  My paper beats your rock!");
            }
                
        //USER SELECTS PAPER
        } else if (userChoice === "Paper") {
            if (compChoice === "rock"){
                return ("You've won! Paper beats my rock");
            }
            else {
                return ("You've LOST! My scissors beat your paper");
            }
        
            //USER SELECTS SCISSORS
            } else if (userChoice === "Scissors"){
                if (compChoice === "rock"){
                    return ("You've LOST! My rock beats your scissors");
                }
                else {
                    return ("You've WON! Scissors prevails!");
                }
            }
            
        
        
        
    };

// run script
compare(ELuser, ELcomp);

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.