Rock Paper Scissors: modifying playGame() to accept user input?

Rather than hard-coding the user’s choice into the body of the playGame() function, I’d like to be able to pass it as an argument.

Would it be appropriate to design the game this way? It would seem to make sense to me, but I haven’t been able to wrap my head around how I would refactor it.

This is the code I’ve got, which is mostly the suggested solution. How would I change it to behave the way I want?

const getUserChoice = userInput => { userInput = userInput.toLowerCase(); if (userInput === 'bomb' || userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') { return userInput; } else { console.log("Input needs to be 'rock,' 'paper,' or 'scissors'."); } }; const getComputerChoice = () => { const randomNumber = Math.floor(Math.random() * 3); switch (randomNumber) { case 0: return 'rock'; case 1: return 'paper'; case 2: return 'scissors'; } }; const determineWinner = (userChoice, computerChoice) => { if (userChoice === computerChoice) { return '\nThis round is a tie!'; } if (userChoice === 'rock') { if (computerChoice === 'paper') { return '\nSorry, you lost!'; } else { return '\nCongratulations, you won!'; } } if (userChoice === 'paper') { if (computerChoice === 'scissors') { return '\nSorry, you lost!'; } else { return '\nCongratulations, you won!'; } } if (userChoice === 'scissors') { if (computerChoice === 'rock') { return '\nSorry, you lost!'; } else { return '\nCongratulations, you won!'; } } }; const playGame = () => { const userChoice = getUserChoice('bomb'); const computerChoice = getComputerChoice(); console.log(`You threw: ${userChoice}`); if (userChoice === 'bomb') { console.log('\nBOOM!\n') console.log('Congratulations, your opponent is dead meat.') } else { console.log(`\nThe computer threw: ${computerChoice}`); console.log(determineWinner(userChoice, computerChoice)); } } playGame();

You can however you have to account for the codecademy environment. Codecademy’s JS environment during the projects and such don’t have a terminal, so you would need to be off site and look up ways to do that.
However as JS is basically used for the web, your best call would be to build a simple webpage, with an HTML form input box to take user input, then assign that input on submit to the userChoice JS variable. The simple HTML form would be really quick to set up, and there would only need to be as much styling as you’d like. The JS can then stay mostly unchanged, just with the userChoice being assigned upon clicking submit, and then outputting the final result to the webpage. This would be the way I’d go to improve this and do what you want it to do.

EDIT: Here’s a quick, unstyled example of what a prospective game framework could look like.

4 Likes

Adding to Adam’s response. As mentioned you won’t be able to do it from the CC environment of this specific project. You will need to setup in your own development environment on your PC. Besides the HTML route the other option is do it in Node.js.

Node handles this in an asynchronous way which may be a concept beyond where you are in the Learn JS course right now. Here is how that is accomplished:
https://www.codecademy.com/articles/getting-user-input-in-node-js

The most friendly way right now if you already learned HTML and CSS, is the route Adam described before.

2 Likes

Thanks, guys. I’m always looking ahead to the next step :slight_smile: