I'm trying to make a RPS game to play in the console. This is what I have so far. At the moment it is only console logging "Do you choose rock, paper or scissors?" and "Error! Try again!"). In other words it is not checking if the user is actually entering 'rock, paper or scissors.' What am I doing wrong?
const userInput = console.log("Do you choose rock, paper or scissors?");
if(userInput ==="paper" || userInput ==="rock" || userInput ==="scissors"){
console.log("Your choice is " `${userInput}`);
} else {
console.log("Error! Try again!");
}
CodePudding user response:
Just change const userInput = console.log("Do you choose rock, paper or scissors?");
to const userInput = prompt("Do you choose rock, paper or scissors?");
CodePudding user response:
If I understand you well you want to create the RPS to be playable in the console. For doing so, you need to declare the input and give it a value. For example:
const userInput = console.log("Do you choose rock, paper or scissors?");
let input = "paper"
if(input ==="paper" || input ==="rock" || input ==="scissors"){
console.log("Your choice is " `${input}`);
} else {
console.log("Error! Try again!");
}
Another way if you want to do it interactive is to use prompt.
const userInput = console.log("Do you choose rock, paper or scissors?");
let input = prompt("Do you choose rock, paper or scissors?");
if(input ==="paper" || input ==="rock" || input ==="scissors"){
console.log("Your choice is " `${input}`);
} else {
console.log("Error! Try again!");
}
