Rock, paper, scissor

Am I doing this incorrectly?
What am I doing wrong? It prints out a statement but acts as a normal console output.


const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();

  if(userInput === 'rock','paper','scissors'){
    return userInput} else {console.log("We're sorry please try again with rock, paper, or scissors.");}
}
getUserChoice('money')
console.log(getUserChoice('money'));

Hello @uhlnoor, sorry, I don’t quite understand what you mean. Could you post your full code as well, please?
There is a flaw in your logical code here:

When doing this, you must specify the object and condition each time, like this:

if (object1 === "condition1" || object1 === "condition2" {

}

If you have a return here:

Then when your code is run, it won’t return undefined, when you console.log() it. As a general rule, you should either have all console.log() in your functions, or return. (That is unless you need to print something, then return a different value. But in your case, you should have one or the other.
I hope this helps!

That is as far as I have gotten with the project, and thank you for your explanation here. I don’t understand the last part. Could you please explain?


const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();

  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput} else {console.log("We're sorry please try again with rock, paper, or scissors.");}

}
getUserChoice('rock');
console.log(getUserChoice('rock'));

When I put the following inside the function, it doesn’t work at all. But when it’s out, I get undefined when falsy

getUserChoice(‘rock’);
console.log(getUserChoice(‘rock’));

The reason you get undefined when there’s a falsy value is because you’re console.log() the result. This logs the result, but does not return any value. This means that if you log the result of nothing (as there is no information being returned when you call the function with a falsy value), you get undefined.

If you remove the console.log() from your function, and have a return, then you won’t need this line:

And you won’t get undefined if there is a falsy value (such as “roc”).
I hope this helps!

Did you mean this?

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();

  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput} else {console.log("We're sorry please try again with rock, paper, or scissors.");}
}

return(getUserChoice('rock'));

If you remove the console.log() from this:

And replace it with a return, you will avoid getting undefined if a falsy value is passed into the function (either falsy or not "rock", "paper", or "scissors".) That way, you can just call the function inside a console.log(), without worrying about undefined:

console.log(getUserChoice("scisors"))

and you will get:

>>"We're sorry please try again with rock, paper, or scissors."

Without undefined at the end.
I hope this helps!

Will what you just helped me with come with experience to me as well, or do I need to go back and reference something and understand it better? Everything you said worked but I want to think of that myself next time.

Hello! It should come with experience, if not, I wouldn’t say you have to go back and revisit things, rather just play around with the concepts. If you know what return does, and you know what console.log() does, then you should be fine, if not, here is a quick reminder:

return:
This hands the data back to the caller; it passes the data that you are returning from the function, and to the rest of the program:

function a() {
return 3;//3 can now be used as a number when this function is called
}
Analogy is you're stuck

Think about it as a website. When you enter something into a form (imagine yourself as the function), you are returning (giving) data to the form. This is what return does; it gives information to the program calling the function.

console.log():
This merely prints information to the console. The information inside this statement is not accessible to the program in any way other than it replicating the data inside it and printing the data onto the console.

Analogy if you're stuck

Imagine writing down information to enter into a form on a website. This is now useful for other people (the console), but the form itself can’t do anything with that information, as you have written it down.

I hope this helps!

1 Like

Thank you for that detailed description. I have trouble remembering what return does but overtime I will get it, I think

1 Like