Click here to Skip to main content
15,917,991 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
function userChoice = prompt("Do you want to choose rock, paper or scissors?");
if (userChoice !== "rock" || userChoice !== "paper" || userChoice !== "scissors") {
    console.log("Invalid choice. Please choose rock, paper or scissors.")
    prompt("Do you want to choose rock, paper or scissors?");
}
console.log("Player: " + userChoice);

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
}
else if (computerChoice <= 0.67){
    computerChoice = "paper";
}
else {
    computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);

function compare(choice1, choice2){
    if (choice1 === choice2) {
        console.log("The result is a tie");
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            console.log("rock wins!");
        }
        else {
            console.log("paper wins!");
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "scissors") {
            console.log("scissors win!");
        }
        else {
            console.log("paper wins!");
        }
    }
};
compare(userChoice, computerChoice);


What I have tried:

var userChoice = prompt("Do you want to choose rock, paper or scissors?");
if (userChoice !== "rock" || userChoice !== "paper" || userChoice !== "scissors") {
    console.log("Invalid choice. Please choose rock, paper or scissors.")
    prompt("Do you want to choose rock, paper or scissors?");
}
console.log("Player: " + userChoice);

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
}
else if (computerChoice <= 0.67){
    computerChoice = "paper";
}
else {
    computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);

function compare(choice1, choice2){
    if (choice1 === choice2) {
        console.log("The result is a tie");
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            console.log("rock wins!");
        }
        else {
            console.log("paper wins!");
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "scissors") {
            console.log("scissors win!");
        }
        else {
            console.log("paper wins!");
        }
    }
};
compare(userChoice, computerChoice);
Posted
Updated 11-Nov-20 22:29pm

1 solution

This doesn't look right:
function userChoice = prompt("Do you want to choose rock, paper or scissors?");

I think you meant to declare the variable like this instead:
var userChoice = prompt("Do you want to choose rock, paper or scissors?");

A function is declared by using it in one of two ways:
function functionname() {

}

var functionname = function() {

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900