Paper Beats Rock Q&A


#1

I have been scanning the forum to try and solve my problem but evidently I can't find something I understand :disappointed: I am doing 4/9 and I have a syntax error. Here's is my javascript:

var userChoice = prompt("Do you chose rock, paper, or scissors?");
var computerChoice = Math.random(1);
console.log(computerChoice)
if (computerChoice < 0.33) {
    computerChoice = "rock";
} else if (computerChoice < 0.67) {
    computerChoice = "paper";
} else (computerChoice < 0.99) {
    computerChoice = "scissors";
}

I hope you will find my problem
Thanks


#2

If you tell the syntax error message the lesson gives you... but put a semicolon after the console.log statement.


#3

I added the semicolon after the console.log but it still tells me I have a syntax error in fact, it tells me this: SyntaxError: missing before statement if that helps.


#4

/* this shoul work */
var userChoice = prompt("Do you choose rock,paper or scissors");
var computerChoice = Math.random();
console.log(" " + computerChoice+" ");
if (computerChoice<0.33) {
console.log("rock");
} else if(0.33 console.log("papper");
} else {
console.log("scrissors");
}


#5

Your last else shouldn't have that condition between parentheses.


#6

var userChoice = prompt("Do you choose rock,paper or scissors");
var computerChoice = Math.random();
console.log(" " + computerChoice+" ");
if (computerChoice<0.33) {
console.log("rock");
} else if (0.33 < computerChoice < 0.66) {
console.log("papper");
} else {
console.log("scrissors");
}


#7

Why shouldn't it be in parentheses?


#8

An else never has conditions (the others one have an if inside their block statements, but no conditions).


#9

Ah ok... but guess what! The THING still tells me that I'm wrong! :rage:


#10

Refresh the page, try, and if persists, show a snapshot.


#11

Ok, I will try to get a snapshot :camera: but I not quite sure if I'll be able to do it right now :clock4: . I will try to do one though...thanks


#12

Ok from what I can see @arccoder77254 is correct and the big problem is the "condition" after the else case. Else is the default case so you pick it anyway if nothing else worked before so it not only redundant to give it a condition it may also lead to errors as it is not expected to see one. If you want one better use else if again.
The thing with the semicolon is valid as well but this does not cause any syntax errors.

Also you can get rid of the 1 in the () of your Math.random(). It is not harmful but might be missleading, in fact this does nothing at all and if you thought that this would set the range from 0 to 1 then no you got to find another way. Additional arguments passed to a function call will just be ignored.

Could you post the updated code? Screenshots are not necessary but a description what exercise you're on and the fixed code might already help.

@sokoljosifi 2 Things please be a bit more descriptive. Is what you're posting a solution or a question? If it is a solution and i need to tell you that it is actually wrong or at least not right. The idea of the exercise is that you assign "rock", "paper" or "scissors" to the variable computerChoice based on the value of a random number. But your code only prints these values but in the end computerChoice would still be that number. You can prove this by adding a console.log computerChoice at the end.

Also this kind of conditions doesn't exist:

0.33 < computerChoice < 0.66

The really crucial thing about it is that it is not wrong it just does the opposite of what you would expect it to do. So javaScript would not throw an error but you might end up with pretty strange results. As it is interoreted like this:

0.33 < computerChoice < 0.66
(0.33 < computerChoice) < 0.66
true/false < 0.66

And as true has a numerical value of 1 and false one of 0 this whole statement will be true for computerChoice being less than 0.33 and false for being bigger than 0.33.

Last but not least if you post code make use of these backtiks or the </> button

` code `
``` code ``` also has syntax highlighting aka color

otherwise your <> can lead to lost code because they are interpreted as matching pairs of formatting tags. As you can see here:

} else if(0.33 console.log("papper");


#13

Ok...here's my updated code:
Is the && computerChoice >= # needed? I forgot to put this. Could this be the problem confusing the Thing?

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

Thanks


#14
} else computerChoice < 0.99 {
    computerChoice = "scissors";
}

to

} else {
    computerChoice = "scissors";
}

And you can ommit the >= 0 and >= 0.34 conditionals.


#15

That helped. It is Solved! :blush:
Thanks a ton :+1: