That is a syntax issue now, you have an extra } before the else if.
Oops,
Thanks so much; I didn’t know you could declare variables within functions… I only thought you could use the parameters.
Thank you for your input!
This is completely random, but what you said hit me with nostalgia so hard. I remember learning that for the first time too!
It’s amazing seeing other people learn how to code and do the same things I did
I thought I was restricted to using only parameters, so I was completely thrown for a loop (lol) when I could declare a variable inside my function ha, but I finally finished the rock, paper, scissors project!
It’s really fun to learn new things!
How long ago did you learn to code?
Nice job! That rock paper scissors project can be a hard one.
I started about 7 years ago, and around six years ago I had that problem. I was not what you call, a fast learned xD
Hey @bhb23955,
I see you asking a lot of good questions recently . Keep up the good work!
You’ll learn coding by doing, trial and error and asking others for help.
Happy coding!
A little hint for forum usage, you can post your code in your posts using the </>
button in the editor.
Press that button and then paste your code between the back ticks.
I’m working on this same project right now. I have the correct code but I do have some questions:
Why can’t I use-
if (userInput === 'rock' || 'paper' || 'scissors') {
return userInput
} else {
return console.log(`You have to choose: rock, paper, or scissors. ${userInput} isn't a valid option.`)
instead of-
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput
} else {
return console.log(`You have to choose: rock, paper, or scissors. ${userInput} isn't a valid option.`)
Because you compare conditions. This is what the syntax describes.
Oh thank you,
I didn’t know that
To elaborate on @janneslohmeijer’s answer…
Your proposed if
looks like this:
if (userInput === 'rock' || 'paper' || 'scissors')
There are three conditions, separated by or
operators, so if any one of them is true
we will execute the code in the if
block.
Your three conditions are:
-
userInput === 'rock'
, which will evaluate totrue
ifuserInput
is a string whose value isrock
, -
'paper'
, which will always evaluate totrue
, -
'scissors'
, which will always evaluate totrue
.
In the context of a boolean operation, a non-empty string is what is referred to as a “truthy” value. It’s not strictly speaking a boolean true
, but it’s value suggests it should return as such. Conversely, an empty string (e.g. let no = '';
) is a “falsey” value, and is treated as a boolean false
.
We can demonstrate this simply:
let t = 'paper';
if (t) { console.log("true"); } else { console.log("false"); } // console.log -> true
Since you have two “truthy” values in your if
, and we only need one of them to be true thanks to the or
operator, you always get the if
block and never the else
. This is not the correct behaviour.
Hope that helps.
I really like this description!
This is very helpful, thanks! I was reading this:
if (userInput === 'rock' || 'paper' || 'scissors')
as a shorthand of accomplishing this->
userInput === 'rock'
userInput === 'paper'
userInput === 'scissors'
I see now why those second conditions were evaluating to “truthy”, due to the fact that they were non-empty strings.
Is there a way to shorthand the code? So that it understands I am asking for a match to any one of those conditions without having to spell them each out explicitly?
Something like:
(userInput === ('rock' || 'paper' || 'scissors'))
In this case the easiest is probably:
if(["rock", "paper", "scissors"].includes(userInput))
EDIT: thought this was python for some reason.
The easiest way I can think to do it is actually the way that @jagking suggested, albeit in JS rather than Python.
let t = 'paper';
function checkThrow(janken) {
let valid = ['rock','paper','scissors'];
if (valid.includes(janken)) {
console.log("Valid throw!");
} else {
console.log("Sorry, try again!");
}
};
checkThrow(t);
Array types inherit from the prototype, which includes the includes() method. We can use this to test for the presence of the provided input in a pre-defined array of acceptable values.
If you wanted to do it a really hacky (and probably thoroughly ill-advised) way, you could strap together a switch
statement like so:
let t = 'paper';
function validateThrow(janken) {
switch(janken) {
case 'rock':
case 'paper':
case 'scissors':
console.log("Valid selection!");
break;
default:
console.log("Invalid choice!");
}
};
validateThrow(t);
I would not advise that you do it that way, though. Essentially what we’re doing with the switch
is having JS match any of the allowed cases, and then “fall through” the switch running everything we come across until we either hit break
or exit the switch… which is very much not good behaviour.
It is, however, a fun example.
Hi guys,
I am having some trouble with my codes and I don’t understand where is the problem.
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’){
return userInput;
}else {
return ‘Error!’
}
}
function getComputerChoice() {
let randomNumber = Math.floor(Math.random() * 3);
if (randomNumber === 0){
console.log(‘rock’);
} else if (randomNumber === 1){
console.log(‘paper’);
}else if (randomNumber === 2){
console.log(‘scissors’);
}
}
function determineWinner(userChoice, computerChoice){
if (userChoice === computerChoice){
return ‘It was a tie!’;
} else if (userChoice === ‘rock’){
if (computerChoice === ‘paper’){
return ‘The computer Wins’;
} else if (computerChoice === ‘scissors’){
return ‘The user Wins’;
}
}else if (userChoice === ‘paper’){
if (computerChoice === ‘scissors’){
return ‘Computer Wins’;
}else if (computerChoice === ‘rock’){
return ‘User Wins’;
}
}else if(userChoice === 'scissors'){
if (computerChoice === 'rock'){
return 'Computer Wins';
}else if (computerChoice === 'paper'){
return 'User Wins';
}
}
}
const playGame = () => {
const userChoice = getUserChoice(‘rock’);
const computerChoice = getComputerChoice();
console.log('Your choice was ’ + userChoice);
console.log('The computer choice was ’ + computerChoice);
}
playGame();
When I run the game, shows this results:
scissors
Your choice was rock
The computer choice was undefined
Why is the computer choice undefined??
I home someone could help me.
Thanks a lot!
was it supposed to be something else? why? did you define it somewhere? (no, then it would be something else) where?
Hi Ionatan,
Have you done this exercise before?
const playGame = () => {
const userChoice = getUserChoice(‘rock’);
const computerChoice = getComputerChoice();
console.log('Your choice was ’ + userChoice);
console.log('The computer choice was ’ + computerChoice);
}
This code was supposed to give me the User Choice and the Computer Choice.
The User Choice was Rock, but the Computer Choice is ‘Undefined’.
Instead of Undefined, it was supposed to give me Rock, Paper or Scissors.
Do you know what is wrong about that?
it is something you wrote isn’t it?
so if it isn’t behaving like you want, you’d turn your attention to it
the undefined is coming from somewhere, so you’d continue looking there, follow it until you find the cause
const determineWinner = (userChoice, computerChoice) =>{
if (userChoice === computerChoice){
return 'This game is a tie';
}
if (userChoice === 'rock'){
if (computerChoice === 'paper') {
return 'sorry computer won!';
} else {
return 'you won!';
}
}
if (userChoice === 'paper'){
if (computerChoice === 'scissors'){
return 'computer won!';
} else {
return 'you won!';
}
}
if (userChoice === 'scissors'){
if (computerChoice === 'rock'){
return 'computer won!';
}else {
return 'you won!';
}
}
};
console.log(determineWinner('scissors, rock'));
console.log(determineWinner('rock, rock'));
Why is my console only logging undefined? please help!!!