Why is my console only logging paper? I’ve called the function several times, but I keep getting paper.
The reason you are always getting paper is because 0 will always evaluate to false, and when it gets to 1 that will always evaluate to true. You want to compare something to see if it is equal to 0, or 1.
Also to properly do a random number betwee a range, see here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
Yours will never be greater than 1 because random gets 0 (inclusive) - 1 (exclusive). So once you times less than 1 by 2 and floor it the greatest you’ll get is 1.
Why does the exercise ask us not to put parameters for the getComputerChoice funtion?
Couldn’t we define the computer parameter to computerInput then set it equal to Math.floor(Math.random()*2); ? then do the condtions === 0, 1, or 2
You don’t need any parameters. You calculate a random number but you don’t store it in a variable or use it to decide if you should return rock, paper or scissors based on it.
A parameter is what is passed into a function. In your () => part the () part is where you would put a parameter. You don’t need to put anything there. A variable would be where you store the random number you generate and would be created inside the function.
const randInt = Math.Floor(*Rest of your code*)
And you then use that variable in the if else statement.
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.