On the exercise Rock, Paper, Scissors on Learn Javascript course (https://bit.ly/2QQDsk7 ), on task number 4:
Test the function by calling it with valid and invalid input, and printing the results to the console. You can delete this when you know your function works.
I’m getting the word ‘undefined’ printed after the ‘Error!’ message. That doesn’t happen when my userInput is either ‘rock’, ‘paper’, or ‘scissors’, only if the argument is something else. I can’t figure out why the undefined is being shown.
This is my code:
const getUserChoice = (userInput) => {userInput = userInput.toLowerCase();
if (userInput === ‘rock’|| userInput === ‘paper’|| userInput === ‘scissors’) {return userInput;
}
else {return console.log(‘Error!’);
}
};
console.log(getUserChoice(‘bird’));
This is what is being printed:
Error!
undefined
What am I doing wrong???
Thank you!
I’m also having this issue at the end:
my code:
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’|| userInput === ‘paper’|| userInput === ‘scissors’) {return userInput;
}
else if (userInput === ‘bomb’) {return userInput;
}
else {return console.log(‘Error!’);
}
};
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 ‘computer wins!’
} else return ‘You win!’
}
if (userChoice === ‘paper’) {
if (computerChoice === ‘scissors’) {
return ‘computer wins!’
} else return ‘You win!’
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’) {
return ‘computer wins!’
} else return ‘You win!’
}
if (userChoice === ‘bomb’) {
return ‘You win!’
}
};
const playGame = () => {
const userChoice = getUserChoice(‘bird’);
const computerChoice = getComputerChoice();
console.log(You threw ${userChoice}.
);
console.log(The computer threw ${computerChoice}.
);
console.log(determineWinner(userChoice, computerChoice))
};
playGame();
It prints:
Error!
You threw undefined.
The computer threw paper.
undefined
Shouldn’t we be predicting a sentence for when the user types something that is not ‘rock’, ‘paper’ or ‘scissors’ too? Where would that go, inside the playGame function?
thanks!!!
mtf
October 16, 2018, 6:57pm
3
This will return undefined
since console.log
does not return anything.
Thank you for your help!
So, should I write that line of code as:
return ‘Error!’
?
mtf
October 16, 2018, 7:50pm
5
Part of functional design is to have functions do one important task, and let the game controller handle traffic.
const getUserChoice = userInput => {
x = userInput.toLowerCase();
if (['rock', 'paper', 'scissors', 'bomb'].includes(x) {
return x;
} else {
return false;
}
};
The logic above searches the array for user input value and returns it if found, else it returns false
.
const playGame = () => {
var userChoice = getUserChoice('bird');
if (userChoice) {
var computerChoice = getComputerChoice();
console.log(`You threw ${userChoice}`);
console.log(`Computer threw ${computerChoice}`);
console.log(determineWinner(userChoice, computerChoice));
} else {
console.log('Invalid input!');
}
};
When the getUserChoice
returns false
the game does not continue.
1 Like
Thank you! Your code looks advanced for me - I’m just starting - but I understand the logic.
1 Like
mtf
October 17, 2018, 4:45am
7
Stick with understanding and don’t try to commit it to memory. It will only create bias and we don’t want that. Bookmark and move on. That you are understanding what you read is all that matters moving forward.
2 Likes
Hi there,
I’m also stuck on #5 in the rock, paper scissors lab. Can you help me?
const getComputerChoice = () => { let randomNumber = Math.floor(Math.random()) * 3);
switch (randomNumber) {
case 0:
return ‘rock’;
case 1:
return ‘paper’;
case 2:
return ‘scissors’;
};
};
console.log(getComputerChoice());
mtf
October 18, 2018, 7:29pm
9
What error message are you getting?
Aside
switch
does not get a semi-colon after its body.
switch (expression) {
}
Minor, though, since it’s not hurting anything.
Thanks that helped. Now, I’m doing part 9 and in a few situations I’m getting “undefined”
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return ‘it’s a tie’;
}
if (userChoice === ‘rock’) {
if (computerChoice === ‘paper’) {
return ‘The computer won’;
}
else {
return ‘You won’;}
if (userChoice === ‘paper’) {
if(computerChoice === ‘scissors’) {
return ‘The computer won’;
} else {
return ‘You won’;}
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’) {
return ‘The computer won’;
} else {
return ‘You won’;}
}
}
};
console.log(determineWinner( ‘scissors’, ‘rock’));
mtf
October 18, 2018, 11:17pm
11
Missing endbrace for outer if.
Aside
For clarity, and to avoid errors, I’d suggest always giving endbraces their own line.
if(condition) { // openbrace on same line
if (state) {
} else { // endbrace and openbrace wrap else
}
}
It’s much simpler to visualize the structure. At a glance we can spot brace related errors.