Computer Choice: Part 2


#1


Hello, i m stuck on this exercise, i read it again lot of times but i can t get the error i did, could someone explain me what i did bad there please ?


The message is : SyntaxError: missing ( before condition


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

var computerChoice = Math.random(0, 1);
console.log(computerChoice);

if (computerChoice >= 0 && computerChoice <= 0.33 ) {
    console.log("rock");
}

if else (computerChoice >= 0.34 && computerChoice <= 0.66) {
    console.log("paper");
}

else (computerChoice >= 0.67 && computerChoice <= 1) {
    console.log("scissors");
}


#2

HI the else statement doens't have a condition

if (condition) {
    #Do something
}
else if (condition) {
    #Do something
}

else {
    #Do something
}

then these lines

console.log("rock");
console.log("paper");
console.log("scissors");

You shoudl set computerChoice to "rock", "paper" or "scissors" instead of printing it.


#3

Your last statement needs to be an else if, or you need to provide no parameters for it.

if(condition){

}else if(condition){

}else if(condition){

}else{

}

(Notice how else doesn't take a condition. It runs through all the ifs and else ifs and when the program doesn't find one of those conditions it runs the else.)


#4

I try to replace console.log by set computerChoice but i still have the same error, do you have an idea ?
And tekkie i dont get your explanation, if i put 2 if else, the else final will be empty, perhaps there is a tip for fill it ?
Thanks in advance for your help
Here is the new code :

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

var computerChoice = Math.random(0, 1);
console.log(computerChoice);

if (computerChoice >= 0 && computerChoice <= 0.33 ) {
set computerChoice("rock");
}

if else (computerChoice >= 0.34 && computerChoice <= 0.66) {
set computerChoice("paper");
}

else {
set computerChoice("scissors");
}

Even when i dont put an other if else it is not working with just if / if else / else.


#5

I had problems with this one as well until I came here and noticed that I had one computerChoice missing.

Here's my code that worked:

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

if (computerChoice <= 0.33) {
    console.log("rock");
}
else if (computerChoice > 0.33 && computerChoice <= 0.66) {
    console.log("paper");
}
else {
    console.log("scissors");
}

The else statement in your code doesn't need the condition. Remember, else doesn't need a condition at all.


#6

It worked thank you :slight_smile:


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.