Build “Rock, Paper, Scissors” in JS

I Paper Beats Rock
i. The Game 1/9
Save and Submit

ii. User Choice 2/9

var userChoice =  prompt("Do you choose rock, paper or scissors?")

iii. Computer Choice: Part 1 3/9

var userChoice =  prompt("Do you choose rock, paper or scissors?")
computerChoice = Math.random()
console.log(computerChoice)

iv.Computer Choice: Part 2 4/9

var userChoice =  prompt("Do you choose rock, paper or scissors?")
computerChoice = Math.random()
console.log(computerChoice)
if (0 < computerChoice < 0.33) {
    computerChoice = "rock";
}
else if (0.34 < computerChoice < 0.66) {
    computerChoice = "paper";
} 
else {
    computerChoice = "scissors";
}

II Compare Function

i. Both choices are the same! 5/9

var compare = function(choice1,choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!"
    }
}

ii. What if choice1 is rock? 6/9

var compare = function(choice1,choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!"
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins"
        }
        else {
            return "paper wins"
        }
    }     
}

iii. What if choice1 is paper? 7/9

var compare = function(choice1,choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!"
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins"
        }
        else {
            return "paper wins"
        }
    }     
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
        return "paper wins"
        }
        else {
            return "scissors wins"
        }
    }
}
      

iv. What if choice1 is scissors? 8/9

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} 
else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} 
else {
	computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);
var compare = function(choice1,choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!"
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins"
        }
        else {
            return "paper wins"
        }
    }     
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
        return "paper wins"
        }
        else {
            return "scissors wins"
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "paper") {
            return "scissors wins"
        }
        else {
            return "rock wins"
        }
    }
}
compare(userChoice,computerChoice);

v. Conclusion 9/9 Save and Submit!

Advertisements

5 thoughts on “Build “Rock, Paper, Scissors” in JS

  1. If anyone gets error in 9/9 you can use this code:

    var userChoice = prompt(“Do you choose rock, paper or scissors?”).toLowerCase();

    while(true){
    if(userChoice === “paper” ||userChoice === “scissors”||userChoice === “rock” ){
    break;
    }
    alert(“Please pick from rock, paper, or scissors”);// you can console.log() this part
    userChoice = prompt(“Do you choose rock, paper or scissors?”).toLowerCase();

    }
    console.log(“Your choice is : “+userChoice);

    Like

  2. var userChoice = prompt(“Do you choose rock, paper or scissors?”);
    var computerChoice = Math.random();
    if (computerChoice < 0.34) {
    computerChoice = "rock";
    } else if(computerChoice <= 0.67) {
    computerChoice = "paper";
    } else {
    computerChoice = "scissors";
    } console.log("Computer: " + computerChoice);

    var compare = function(choice1, choice2){
    if(choice1===choice2){
    return "The result is a tie!";
    }
    else if(choice1==="rock"){
    if (choice2==="scissors"){
    return "rock wins"
    }
    else {
    return "paper wins"
    }

    }
    else if(choice1==="paper"){
    if(choice2 ==="rock"){
    return "paper wins"
    }
    else {
    return "scissors wins"
    }
    }
    else if(choice1==="scissors"){
    if (choice2="paper"){
    return "scissors wins"
    }
    else {
    return "rock wins"
    }
    }
    }
    compare(userChoice, computerChoice)

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s