Home » Javascript » JavaScript Rock Paper Scissors game

JavaScript Rock Paper Scissors game

Posted by: admin November 1, 2017 Leave a comment

Questions:

For a task to do with some uni work, I have to make a Rock, Paper, Scissors game using JavaScript. We have to do it variable based and it has to use a while loop.

The game needs to end when either the computer or the user has won three games. Upon running I find an error on line 48 and don’t know how to fix it, nor am I sure on how to properly implement the loop. Help would be appreciated.

var cpuWins = 0;
var userWins = 0;
var totalWins=0;

while(totalWins!=5){

    var userChoice = parseInt(prompt("Enter 1 for Rock, 2 for Paper, 3 for Scissors"));
    var cpuNumber = Math.random();
    
    if (cpuNumber <= (1/3)){
       cpuChoice = "Rock";
    }
    else if (cpuNumber  <=(2/3)){
       cpuChoice = "Paper";
    }
    else{
       cpuChoice= "Scissors";
    }




    if (userChoice===1){
        if (cpuChoice==="Scissors"){
            userWins++;
        }
        else if(cpuChoice==="Paper"){
            cpuWins++;
        }
        else{
            alert("It was a tie");
        }
    }


    if else(userChoice===2){
        if (cpuChoice==="Rock"){
            userWins++;
        }
        else if{
            cpuWins++;
        }
        else{
            alert("It was a tie");
        }
    }

    else{
        if (cpuChoice==="Paper"){
            userWins++;
        }
        else if(cpuChoice==="Rock"){
            cpuWins++;
        }
        else{
            alert("It was a tie");
        }
    }

    totalWins++
}
Answers:

Look at the line you mentioned, 48, what’s wrong with it?

else if{ needs a condition, which should be else if(cpuChoice==="Paper"){.

Questions:
Answers:

As was mentioned there was just a few small syntax errors with your else if statements. See the snippet below:

var cpuWins = 0;
var userWins = 0;
var totalWins=0;

while(totalWins!=5){

    var userChoice = parseInt(prompt("Enter 1 for Rock, 2 for Paper, 3 for Scissors"));
    var cpuNumber = Math.random();
    
    if (cpuNumber <= (1/3)){
       cpuChoice = "Rock";
    }
    else if (cpuNumber  <=(2/3)){
       cpuChoice = "Paper";
    }
    else{
       cpuChoice= "Scissors";
    }




    if (userChoice===1){
        if (cpuChoice==="Scissors"){
            userWins++;
        }
        else if(cpuChoice==="Paper"){
            cpuWins++;
        }
        else{
            alert("It was a tie");
        }
    }


    else if (userChoice===2){
        if (cpuChoice==="Rock"){
            userWins++;
        }
        else if(cpuChoice==="Scissors"){
            cpuWins++;
        }
        else{
            alert("It was a tie");
        }
    }

    else {
        if (cpuChoice==="Paper"){
            userWins++;
        }
        else if (cpuChoice==="Rock"){
            cpuWins++;
        }
        else{
            alert("It was a tie");
        }
    }

    totalWins++
}

Questions:
Answers:

You had syntax errors:

else if{

was not correct, you need to specify a criteria there

if else

is incorrect, the correct order is

else if

I have only corrected the syntax errors, the logic should be figured out by you

var cpuWins = 0;
var userWins = 0;
var totalWins=0;

while(totalWins!=5){

    var userChoice = parseInt(prompt("Enter 1 for Rock, 2 for Paper, 3 for Scissors"));
    var cpuNumber = Math.random();

    if (cpuNumber <= (1/3)){
       cpuChoice = "Rock";
    }
    else if (cpuNumber  <=(2/3)){
       cpuChoice = "Paper";
    }
    else{
       cpuChoice= "Scissors";
    }




    if (userChoice===1){
        if (cpuChoice==="Scissors"){
            userWins++;
        }
        else if(cpuChoice==="Paper"){
            cpuWins++;
        }
        else{
            alert("It was a tie");
        }
    }


    else if(userChoice===2){
        if (cpuChoice==="Rock"){
            userWins++;
        }
        else if (cpuChoice ==="Scrissors"){
            cpuWins++;
        }
        else{
            alert("It was a tie");
        }
    }

    else{
        if (cpuChoice==="Paper"){
            userWins++;
        }
        else if(cpuChoice==="Rock"){
            cpuWins++;
        }
        else{
            alert("It was a tie");
        }
    }

    totalWins++
}