Where am I getting wrong?


#1

hey people,
I would like to some help with my code:

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(choise1, choise2){

if(choise1 === choise2) {
return "The result is a tie!";
}

else if(choise1 === "rock") {

if(choise2 === "scissors") {
return "rock wins";
}
else {
return "paper wins";
}
}
else if(choise2 === "paper") {
if(choise2 === "rock") {
return "paper win";
}
else {
return "scissors wins";
}
}
}
else if(choise1 === "scissors") {
if(choise2 === "rock") {
return "rock win";
}
else {
return "scissors wins";
}
}
}
console.log(compare(userChoice, computerChoice));

I still have syntax error. what is wrong in this code?

thank you!


#2

Hey @tomer_vagenfeld,

else if(choise2 === "paper") {    
    if(choise2 === "rock") {        
        return "paper win";
    }else {     
        return "scissors wins";    
    }
}
} // This is an extra bracket here

And in the very end add a semicolon to finish off your function

        else {     
            return "scissors wins";
        }
    }
} // Semicolon here

#3

hey @cssblaster73027 thank you for your reply!

I tried what you wrote but I'm not sure I'm getting it.

I made some changes according to your answer, but still it is't correct yet.

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(choise1, choise2)
{

if(choise1 === choise2) {
return "The result is a tie!";
}

else if(choise1 === "rock") {

if(choise2 === "scissors") {
return "rock wins";
}
else {
return "paper wins";
}
};
else if(choise2 === "paper") {
if(choise2 === "rock") {
return "paper win";
}
};
else {
return "scissors wins";
}
}
};
else if(choise1 === "scissors") {
if(choise2 === "rock") {
return "rock win";
}
else {
return "scissors wins";
}
}
};
console.log(compare(userChoice, computerChoice));

Thank you very much :smile:


#4

You added semicolons everywhere now and the if/else statements are a little messed up, i suggest you to use the first code you provided and do the two change i gave you, nothing else.
I post your first post here and i will write in the comments what is incorrect.

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(choise1, choise2){
    if(choise1 === choise2) {
        return "The result is a tie!";
    } else if(choise1 === "rock") {
        if(choise2 === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
    } else if(choise2 === "paper") {
        if(choise2 === "rock") {
            return "paper win";
        } else {
            return "scissors wins";
        }
    } // This bracket is doing nothing, but causing you trouble, delete it
    } else if(choise1 === "scissors") {
        if(choise2 === "rock") {
            return "rock win";
        } else {
            return "scissors wins";
        }
    }
} // the only semicolon that you had missing is from here. Add a semicolon here

#5

your spelling choice with an "s" instead of a "c" like you defined in your function.


#6

THANK YOU! it's works.
the thing is, i'm not sure where should I put semicolons and where it's unnecessary.


#7

@mseligma actually he makes a function where the parameters are with "s" and he uses the s throughout the function, so the code will work, even though there are spelling mistakes.

@tomer_vagenfeld I can't give you a good explanation, but i found an old codecademy post where Alex explains a little about using semicolons. When (not) to use semicolons