Rock Paper Scissors game

Why wasn’t a user able to actually input something in this exercise with a prompt window? It doesn’t make much sense to me. Also if you were going to let someone enter there choice with a prompt window how would you go about doing it?

const getUserChoice = userInput => {
let getUserChoice = userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘scissors’ || userInput === ‘paper’){
return userInput;
} else {
console.log(‘Error, please type: rock, papper, or scissors.’);
}
};

let getComputerChoice = () => {
let randomNumber = Math.floor(Math.random() * 3)
switch (randomNumber){
case 0:
return ‘rock’;
case 1:
return ‘paper’;
case 2:
return ‘scissors’;
}
};

let determineWinner = (userChoice, computerChoice) =>{
if (userChoice === computerChoice){
return ‘This game is a tie!’;
}
if (userChoice === ‘rock’){
if (computerChoice === ‘paper’){
return “sorry computer won”;
} else {
return “congratulations, you won!”;
}
}

if (userChoice === ‘paper’){
if (computerChoice === ‘scissors’){
return “Sorry, computer won”;
} else {
return “Congratulations, you won!”;
}
}

if (userChoice === ‘scissors’){
if (computerChoice === ‘rock’){
return “Sorry computer won”;
} else {
return “congratulations, you won!”;
}
}
};

console.log(determineWinner(‘rock’, ‘scissors’));

//console.log(determineWinner(‘rock’, ‘scissors’));

const playGame = () => {
let userChoice = getUserChoice(‘paper’);
let computerChoice = getComputerChoice();
console.log('You threw: ’ + userChoice);
console.log('The computer threw: ’ + computerChoice);

console.log(determineWinner(userChoice, computerChoice));
};

playGame()

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Hello @py6487844868, welcome to the forums! There are two main ways to get user input if you’re using JS: use the prompt function, or use an HTML form. Since you’re using the console anyway, I would suggest using prompt. It accepts a string parameter which it logs to the console, then records any input as a string:

let varInput = prompt("What would you like to say?")

I hope this helps!

1 Like

Usually getting a prompt is more involved, which is why I guess they omitted in this lesson. @codeneutrino is absolutely correct that prompt is maybe the quickest way.

HTML forms could involve creating either text input boxes, or buttons, (or… radio buttons haha), or even dropdowns to take in your input. It is a good side-exercise to do if you’re interested.

Conversely, you could do it completely via text through node.js as well.

1 Like

For the actual rock paper scissors game i’m not sure what variable I would put the prompt function in to let people actually choose if they want to play rock paper or scissors. Having in defined in the code seems not practical

You could have it the getUserChocie function.

1 Like