Output the winner of Rock, Paper, Scissors


First, it should be noted that I am a complete noob at programming. JS happens to be the first programming language I decided to learn, and I like trying to get the most possible out of every exercise.

I successfully wrote the code to complete the lesson

var userChoice = prompt("Do you choose rock, paper, or scissors?");
var computerChoice = Math.random(); 
if (computerChoice < .33) {
    computerChooice = "rock";
} else if (computerChoice > .67) {
    computerChoice = "scissors";
} else {
    computerChoice = "paper";

but I would like for the computer to tell me whether I won or lost. I was hoping something like this (obviously expanded to include all of the possibilities) would work:

if (userChoice = "scissors", computerChoice = "paper") {
    console.log("You win!");
else {
    console.log("You lose.");

but I don't think that's the correct way to give two inputs to if(); and I'm not even sure something like that would work at all.

How can I make this code work properly?
I appreciate any input, but please keep in mind that I am still a beginner and would love detailed explanations along with any answer.


No you're on the right track! The lesson only wants you to make it work, it doesn't care if you spruce it up to make it look more presentable for the player! Just as long as you make sure which choice is the player and which one is the computer.

The only problem you're running into is with the if statement here:

if (userChoice = "scissors", computerChoice = "paper") { /* Also change the equals signs
so they aren't assignment operators as they are now! */

You'll want to change that to have a "nested if statement" like this:

if (userChoice === "scissors") {
  if (computerChoice === "paper") {
    //Code here!

I'd suggest finishing the lessons as they'll explain in more detail as you go through them. This will also explain how it wants you to structure your if statements, and how all of this will be inside of a function!

I hope that this helps for now!


@lolman can we use -

if (userChoice = "scissors" && computerChoice = "paper"){

is this true??


Yes, and this can be used but I would not suggest it for this sort of if statement.

otherwise we would have to write out all of the following:

if (userChoice === "scissors" && computerChoice === "paper"){ ... }
else if (userChoice === "scissors" && computerChoice === "rock"){ ... }
else if (userChoice === "rock" && computerChoice === "paper"){ ... }
else if (userChoice === "rock" && computerChoice === "scissors"){ ... }
else if (userChoice === "paper" && computerChoice === "rock"){ ... }
else { ... }

This not only doesn't look clean, but it's tedious and hurts my brain to think about. Nested if statements are best used when two different conditions have a wide variety of choices between them.

That's not to say you can't do it however you want, but the least amount of code is the most efficient and that's what programmers shoot for.


I'd agree with what @lolman suggested. Also @lolman and @ankurlvv be careful with the =, == and === it's probably just a typo as lolman already corrected it the post above, but it can really make a difference if you use a comparision or an assignment operator :wink:


Right, I wasn't paying attention to the assignment operators so my code really wouldn't work.


Thank you so much for the explanations! I knew I was getting ahead of myself, but I didn't anticipate that there would be a much easier way to do exactly what I was hoping without writing out each possibility. I actually made a list of all of the different possible outcomes and expected that I would have to write out a long list of if, else if, else statements. I also really appreciate the acknowledgement of several ways to achieve the same purpose.

I am, however, still a little unclear on the differences among =, ==, and ===, partially because they haven't been fully explained yet but also because I don't quite understand everything about declaring variables (how exactly to do it, when i need to use var, what global variables are, etc).

I guess I just need to try to let myself get through the simple exercises without trying to get too far ahead of what I know how to do. It's just a thing about the way I learn - I wish I could learn everything all at once so I invent ways to go further than the lesson expects.

Thanks again for the responses! I'm very excited to go further into the language.


Wow, nested if statements are literally the very next exercise. Sure wish I had more time lately to just sit down and do these lessons...


Clarification of technical terms:

var name;

definition. Giving a variable a type by assigning a value which has a type:

name = value;


var name = value;

The declaration is done once before the first use to say "I want space for a value here". Global vs Local is a bit more complex better see the examples in the function track for more. In general the idea is that functions are sort of like sub programs and therefor can have their own set of variables. So if you use var inside of a function the declared variable will only exist in the function. On the otherhand variables declared in the scope where the function is declared as well e.g.

//same scope level
var name = 42;
var name2 = function(){}

can be used inside the function. So as the varaibles inside the function only exist there, they are called local and as the variables declared outside of it also exist inside of the function they are called global. A special thing happens when you declare a variable existing outside of a function inside of a function again, because then the local variable makes the global variable of the same name not accessible which is called shadowing, not that important for now but you might keep it in mind to avoid errors.

Last but not least the =,== and ===
= is the assignment operator that stores the value on the right of it in the variable on the left of it.
== and === are comparision operators which both compare the value but === is more strict and compares the type as well e.g. numerical strings as "42" have the same value as numbers so

"42" == 42 --> true

but obviously the types are not equal "number" != "string" so:

"42" === 42 --> false

Hope this gives you a little overview.


Yeah that was exactly the type of overview I was looking for, presented in just about the same way I would hope it would be which is really cool to see. Thank you.

Sounds like == is "similar" and === is "congruent".