Rock Paper Scissors project - Catching input error


#1

Hi all! I have just finished the first Rock Paper Scissors project and everything seems to be working just fine. I wanted to add some code to catch errors were the user to enter something that is not “rock”, “paper” or “scissors”.
I managed to make an alert pop up whenever there is invalid input, and then another prompt. The program, however, just record “undefined” and goes on. I really don’t know where to go from there. Any advice?
Here is my code:

Document let playerScore = 0; let cpuScore = 0;
function cpuPlay(){
        const RPS = ["rock", "paper", "scissors"];
        let rand = RPS[Math.floor(Math.random()*RPS.length)];
        return rand;

}



function oneRound(cpuSelection, playerSelection){
    switch (cpuSelection + "|" + playerSelection) {
           case "rock|paper":
           playerScore++;
           return "You win! Paper beats rock";
           
           break;
           case "rock|scissors":
           cpuScore++;
           return "You lose! Rock beats scissors";
           
           break;
           case "paper|rock":
           cpuScore++;
           return "You lose! Paper beats rock";
           
           break;
           case "paper|scissors":
           playerScore++;
           return "You win! Scissors beat paper";
            
           break;
           case "scissors|rock":
           playerScore++;
           return "You win! Rock beats scissors";
           
           break;
           case "scissors|paper":
           cpuScore++;
           return "You lose! Scissors beat paper";
           
           break;
           default:
           return "It's a tie!";
       }



}
function oneGame(){
    alert("Round " +(count+1));
    console.log("Round " +(count+1));
    function getSelection(){
        input = prompt("Choose rock, paper or scissors?").toLowerCase();
        if (input !== "rock" || input !== "paper" || input !=="scissors"){
            alert("You have to choose rock, paper or scissors!")
            input = prompt("Choose rock, paper or scissors?").toLowerCase();
    
        }
    }

    const playerSelection = getSelection();

    const cpuSelection = cpuPlay();

    console.log("You play " +playerSelection);
    console.log("The computer plays "+ cpuSelection);
    console.log(oneRound(cpuSelection, playerSelection));
}
    
for (count = 0; count < 5; count++){
    oneGame()
}

function winner(){
    console.log("Your score is " + playerScore);
    console.log("The computer score is " + cpuScore);
    if (playerScore > cpuScore){
        return "You win!"
    }
    else if (cpuScore > playerScore){
        return "You lose!"
    }
    else {
        return "It's a tie!"
    }
}

console.log(winner())
</script>

#2

hi. i would like to help but can you give me more details such as which is causing the error?


#3

I’m trying to d this: if the player writes something that is not the words rock, paper or scissors, then the program alerts “You have to choose rock, paper or scissors!” and then prompts again for a word. What I wrote works except that after the player writes a word for the second time, it seems that the program doesn’t substitute the value but remember “undefined”.
I’m not sure if I was able to explain it more clearly.
This is the bit I modified:

function getSelection(){
input = prompt(“Choose rock, paper or scissors?”).toLowerCase();
if (input !== “rock” || input !== “paper” || input !==“scissors”){
alert(“You have to choose rock, paper or scissors!”)
input = prompt(“Choose rock, paper or scissors?”).toLowerCase(); }
}


#4

it seems that you are defining the function within the [oneGame] function.