Prompt in Rock, Paper, Scissors


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-javascript-functions/projects/rock-paper-scissors?r=master

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
I am trying to get a prompt to open up and ask whether I want to put in rock paper or scissors. For some reason whichever way I try prompt is undefined my code is below

```

const getUserChoice = (userInput ) =>
{ var userChoice = prompt(‘rock, scissors or paper?’);
userInput = userInput.toLowerCase();

if (userInput === 'rock' || userInput ==='paper' || userInput === 'scissors')
{return userInput;
} else {
  console.log('Enter either rock, paper, or scissors.');
}

}
const getComputerChoice = () =>{
let 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 “It’s a tie!”
}
if (userChoice === ‘rock’) {
if (computerChoice === ‘paper’)
return ‘You Loose!’;
}
if (userChoice === ‘paper’) {
if (computerChoice === ‘scissors’)
return ‘You Loose!’;
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’)
return ‘You Loose!’;
} else {
return ‘You Won!’;
}
}

const playGame = () =>{
const userChoice = getUserChoice(‘rock’);

const computerChoice = getComputerChoice();
console.log('You threw: ’ + userChoice);
console.log('The computer threw: ’ + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}

playGame();

<do not remove the three backticks above>

#2

Hi @erne22,

Because this line of code:

const playGame = () => {
const userChoice = getUserChoice('rock'); //<== this line is making sure your user choice is always 'rock'
  
//the rest of code

}

When you want a prompt to ask a user choice, consider if the code above serve its purpose.


When you trace back to the getUserChoice() function in your code (the very top one), there are couple of things you need to change:

const getUserChoice = (userInput )=> {  //<== this part?
   var userChoice = prompt('rock, scissors or paper?');   //<== this part?
   userInput = userInput.toLowerCase();    //<== this part?

   if (userInput === 'rock' || userInput ==='paper' || userInput === 'scissors') {
      return userInput;
   } else {
      console.log('Enter either rock, paper, or scissors.');
   }
};
  • If there’s a prompt to ask a user choice, do you need a parameter userInput in the getUserChoice() function?

    const getUserChoice = (userInput ) =>{  //<== is the userInput needed?
    
  • Your prompt is correct, but you saved the answer in a variable named userChoice, and does the function returned userChoice or userInput? If the function return userInput (output of the function), then your userChoice answer is not returned and not output from the function.

  • Look below the use of toLowerCase(), what value is in userInput compared to userChoice? what or which variable’s value that you want to turn it into lower case.

    { var userChoice = prompt('rock, scissors or paper?');  
    userInput = userInput.toLowerCase();
    

Hope that serves as a pointer to you. If you have more questions, please post your code by using the </> symbol and paste your code inside, it will ease anyone who decide to help to look over your code.

Thank you. :slight_smile:


#3

It seems to be that with-in the course lesson I am not allowed to use prompt?
No matter how I do it I have not been able to prompt myself. Within the in-browser console for the lesson.

Also I followed the lesson when I set user </>const getUserChoice = (userInput ) =>{</>

For those of you who may be reading this in the future it seems this is a bug within the system. As the lesson teaches to write the code as I have written above, although it does not include a way to prompt.


#4

Hi @erne22,

I posted my suggestion based on your code to what I understand you wanted a prompt to ask for a user choice, but it’s not working meaning what you inserted in the prompt box is not showing in the result. (I didn’t know about the matter prompt box is not showing up).

As the way how your code is written above, even the prompt box is showing, you cant arrive into what you want to achieve. Since I don’t have access to the project you had, you can try paste your code in https://repl.it/languages/javascript

to see if your code is working with the prompt box showing out or not, other than that, I have no idea if the environment is preventing any prompt box from showing as I do not have the access to it.

The link repl.it is how I tested your code and modify it in order to have a prompt box asking an input from user, then compared with the computer choice to determine the winner, the code that need amendments are what I mentioned in previous post.

Maybe some other who have access to the project can help you better with your situation. I think you should have access to the Pro advisors’s live help as well.

Cheers :slight_smile:


Prompt not allowed in some lessons?
#5

Its okay I ran the code in repl.it and it works fine.

for whatever reason it does not work on the lesson I am working on.
Now the next step from here is learning how to print the console log into an html page. I appreciate your help @codexthon


#6

If the lesson is indeed asking you to write a prompt code (I can’t verify this) but the prompt box is not showing at all in CodeCademy learning environment, you can try submit a bug report to the team (should be a link in the exercise), or try consult the Pro Advisors what would they suggest for your situation.

Happy Coding @erne22, you’re welcome. :slight_smile:


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.