I’m having issues getting the Rock Paper Scissors project to run correctly. I’m at the point where I have put in the user input and the computer input. After some issues, I watched the help video, and even put down the exact code in the video. In the video, the code executes so the computer returns either “rock”, “paper”, or “scissors”. Here is the code I used, but when I run it, it comes back as “undefined”. What am I doing wrong?
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if(userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’) {
return userInput;
} else {
console.log(‘Error’);
}
}
const getComputerChoice = () => {
const randomNumber = Math.floor(Math.random * 3);
switch(randomNumber) {
case 0:
return ‘rock’;
case 1:
return ‘paper’;
case 2:
return ‘scissors’;
}
}
console.log(getComputerChoice());
Hello, @course5306306327 . Welcome to the forum!
To use Math.random()
, you need to include the empty ()
's. The empty ()
's call the function.
Also, for future posts where you need to paste in your code, please refer to the following:
THANK YOU @patrickd314 for this!!
Code that is posted without the indentations, whitespace and special characters preserved, is very difficult to read, and cannot easily be tested by those trying to help you.
The Codecademy forum provides a quick and easy way to post properly-formatted code. All you need to do is look for the </> icon in the menu bar which appears at the top of the text box while you type.
Press Enter to go to a blank line, click that icon, and you will see this:
Just copy and paste your code directly from the editor into that highlighted portion, and all of the nicely-readable formatting will be preserved! Moreover, it will be in a format that can be directly copied and pasted into an IDE for testing.
The same icon can be used for highlighting small code snippets in-line for clarity.
1 Like