Rock, Paper, Scissors part 4/9 "Computer Choice: Part 2"


#1

This is my non-working code:

var userChoice = prompt("Do you choose rock, paper, or scissor?");
var computerChoice = Math.random();

console.log(computerChoice);

if (computerChoice<.33) {
    computerChoice="rock";
} 

else if (.34<computerChoice<.66) {
    computerChoice="paper";
} 

else (.67<computerChoice<1) {
    computerChoice="scissors";   
}

I keep getting a return of Syntax error: unexpected Token {

I have checked and all of my brackets are in order and there are no extras, what am I doing wrong?


#2

There are 2 problems.
First: JavaScript doesn't know a < x < b and it will be interpreted as (a < x) which is true or false depending on x and after this (true/false < b). And as true and false have numerical values of 0 and 1 you can go through it and see that it does almost the opposite of what you expect it to do. So you could either google for the boolean operators && and || to make use of 2 statements in a condition. Or you could think if you really need to conditions to specify a range from a to b as Math.random already defines a range from 0 to almost 1. Which is probably the way it is planned as && and || will be introduced later.

Second: else has no condiition. Else is the case that is chosen when nothing else worked so if you want to further specify this you could use else if and if you don't need this then else is fully sufficient without condition.


#3

Hello @haxor789 , I am also stuck in the same place... could anyone please take a look at my non-working code and give me suggestions? I'd appreciate it! :slightly_smiling:


console.log("Let's play Rock, Paper, Scissors");
    var question = prompt("Do You Choose Rock, Paper or Scissors?"); 
   var userChoice = ("rock");
   var computerChoice = function (result) {
       return result = Math.random();
   };
   if(computerChoice = (>=0 && <=0.33)) {
       console.log("rock");
   } else if(computerChoice = (>=0.34 && <=0.66)) {
       console.log("paper");
   } else(computerChoice = (>=0.67 && <=1)) {
       console.log("scissors");
   }

error message says: SyntaxError: expected expression, got '>='


#4

First why did you make computerChoice a function? I mean practicing functions isn't that bad but if it only returns Math.random you could as well use Math.random :slightly_smiling:
When you're done with the exercises here 9/9 you can as an improvement wrap you choosing parts in a function but for now I'd stick to just using Math.random.

Also keep in mind that computerChoice is a function in this statement (where a number is expected):

if(computerChoice = (>=0 && <=0.33)) {

And as said before javaScript doesn't know context so well so this will be evaluated one by one:

computerChoice =  // assign what comes after = to computerChoice
(>=0 && <=0.33)  // nothing >= 0 already first error AND nothing <= 0.33 second error

So here you need to go the long way if(computerChoice <= x && computerChoice >= y) but as said maybe think about if the second condition is even necessary to get the right range (it's not). And last but not least:


#5

Thank you :blush: I corrected that code so many times before I decided to ask for help that in the end I think I lost it... lol... I appreciate your explanation


#6


this code worked try it


#7

== is the correct way to say "greater than or equal to".


#8

No that is the correct way to check if in A == B, A and B have the same value. If yes true if not false. greater than (>) or equal to (=) would be >= pretty straight forward, isn't it? Also for the equal to part you could use == which compares the values or === which compares both values and types.


#9

Need some halp.please)

var userChoice = prompt("Do you choose rock,paper or scissors?");

var computerChoice = Math.random();

console.log(computerChoice);

if (computerChoice = (0 => 0.33))
{
console.log(computerChoice = "rock");
}else if {
computerChoice = (0.34 => 0.66);
console.log(computerChoice = "paper");
}else {
computerChoice = (0.67 >= 1)
console.log(computerChoice = "scissors");}

There was a problem with your syntax.


#10

this is the way it worked for me...

var computerChoice = Math.random();
console.log(computerChoice);

if (computerChoice < 0.33) {
computerChoice = "rock";
}

else if (computerChoice < 0.66) {
computerChoice = "paper";
}

else{
computerChoice = "scissors";
}

console.log(computerChoice);


#11

Thanks for the code!!
I got stuck on this portion too. I think I found out the reason why most people are getting wrong, including myself. I realized the last code portion else { computerChoice = "scissors"} essentially is covering the rest of the portionfrom 0.67 to 1 without having to mention the parameters. Because it's and if/ else if/ else, the last else is saying, "I'll cover whatever is left over after splitting the 1 up." Therefore else covers between 0.67 and 1. You don't really have to mention the codes between 0.67 and 1.


#12

I am running into trouble on the last step of this, and I checked the locked post "What if...scissors? What am I doing wrong?" I've compared obsessively, and I really think my code exactly matched the suggested code that supposedly works, but I keep getting this message: "Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and rock". First of all, that message seems to suggest I want an answer of 'undefined', which I don't think I do? And secondly, again, I've checked repeatedly, and mine seems to match.


#13

When you reference a thread it would be wise to use the "share a post button" of that thread or to post the link
Also if you compare your code to another code it would as well make sense to post you code and if this is not related to this question than it would also help to open a new own. :slight_smile:

Anyway, concerning the error message I'd guess that you should ignore the undefined part and focus more on the

Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and rock

highlighted parts. Meaning the error message gives you a rather specific area in the code where something unexpected is happening. So maybe just check this branch to see if the outputs match the intuition.


#14

This is the discussionI was referring to. Aside from some difference in indent style, the code I had was identical to that shown by 0xb4dc0ded. I can copy and paste both below if that would help, but I really went over it with a fine tooth comb checking every bit of punctuation, etc, and I was worried pasting such a long thing would be annoying.


#15

Here is my code.
Never mind - finally found my mistake, after 47 millions times checking it. Is this really how coding works, going blind looking for these little typos? I'll leave it here if anyone wants to go all Where's Waldo on it.

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if (computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    } else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
    } else if (choice1 = "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
    } else {
        if (choice2 === "paper") {
            return "scissors wins";
        } else {
            return "rock wins";
        }
    }
}
compare(userChoice, computerChoice);

#16

else if (choice1 = "paper") {

this = instead of the appropriate == assigns paper to choice1 instead of comparing them. The problem is that this statement:

choice1 = "paper"

has a value of "paper" because that is the value you assign here. And if you dig a bit deeper (not covered in the track) you might find this:

https://www.sitepoint.com/javascript-truthy-falsy/

telling you that a non empty string is evaluated as if it meant true. So what happens is that you pick this case any time you encounter his condition and for "scissors" and "rock" as input the output is "paper wins" which is quite "interesting".

PS: To test this code you can also replace:
compare(userChoice, computerChoice);
with:
compare("scissors", "rock");
or whatever code you want to test.

Hope this helps to solve your problem.


#17

why would you not use return for this task? please see code below.

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


#18

Post wrongly, please delete.


#19

Can you explain how this is posted wrongly?


#20

Thanks! That "truthy-falsy" link was helpful in understanding the various combinations of equal signs and their use. I had actually passed my code on to the teacher who is learning to teach computer science at our school, who passed it on to the professors helping her, and they changed both my === and = to == . It was interesting to see the reason for using the === instead of ==.