4.Computer Choice Help please!


var userChoice = prompt("Do you choose rock, paper or scissors?")
var computerChoice = Math.random();
console.log (computerChoice);
if(computerChoice = (>=0 && <=0.33)) {
computerChoice ="rock";
else if(computerChoice =(< 0.66 && =>0.34)) {
computerChoice = "paper";
else(computerChoice (=>0.67 && =< 1)) {
computerChoice = "scissors"

keeps giving syntax error idk what to do


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

think about what this would mean and explain what you think it does. Also javaScript is not good with handling context.


Hey godyaboi... not sure if you figured out the syntax error or not but I think the console is getting a little tweaked out by some of the context.
I have seen other people use the ( && ) to designate the number being between 2 other numbers. I am just not sure it has to be that complicated given the extra parentheses etc.

I read some other Q & A's regarding this lesson because I was getting nothing but syntax errors and wrong answer. Anyway, some other users were keeping it clean and simple just using (<, >, >=). Not sure if this helps but here is the code I used below to complete this section of the problem. It worked for me, but I am sure there are many ways to complete it! Hope this helps!!

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

var computerChoice = Math.random();

if (computerChoice < 0.33) {
computerChoice = "rock";
else if (computerChoice >= 0.34) {
computerChoice = "paper";
else {
(computerChoice > 0.67)
computerChoice = "scissors";

I think I read something where if you look at like a standard if/else problem, not using the && to designate "in between" numbers it will makes more sense because && is not needed to complete the problem.
So basically I believe the computer will break it down like this.

Lets say we type in "paper" when the prompt gets brought up and the number (between 0 and 1) gets randomly assigned by Math.random() and its .589611196513.
1.) The computer will read .589611196513 and take it through the if statement and begin by seeing if it is < 0.33 (or "true"). In this case .589611196513 is > than 0.33 so the statement is "false".
2.) It will now take this number .589611196513 through the second subset if/else statement which is really the else/if portion and see if that number is >= to 0.34 which it is so it will read this as a "True" statement in the sub else/if statement and print out "paper" in the console.
3. Had this number been > 0.67 when it was put through the subset else/if portion the computer would have read the number as "False" (because "true" was >= 0.34) and printed "scissors" in the console. The computer cuts off the "true" statement (or paper) right when the number becomes greater than 0.67 and will remain "true" between 0.34 and 0.67.

I hope this made sense. This is the way I am understanding it, I am still learning this stuff too but this makes sense to me... but I am sure there are many ways to do this problem!


****Correction: I changed my "paper" and "scissors" values to

Paper: else if (computerChoice <= 0.66) {


Scissors: else (computerChoice >= 0.67)


I wrote something over there concerning this question on why && are not necessary:

About the false answers, ask the one that gave the answer to explain it and/or flag it.
With the fix of the second post it might work just some hint else does not need a condition but deals automatically with everything (else) that is left.


Thank you ever so much for the explanation that's helped me so much with this really appreciate it! :slightly_smiling:


Thanks man really helped!


I just did computerChoice === 0 - 0.33 for the if, computerChoice === 0.34 - 0.66 for the else if, and nothing for the else statement. It checks out logically so it worked for me.


although the next exercise does use <0.34 <=0.67 :stuck_out_tongue:


From what I can see:

computerChoice === 0 - 0.33

first computes the right side which is -0.33 and then compares it to the left side which is always > 0 so you get always false and pick always scissors. What did you expect to happen logically?