unexpected token else codecademy - javascript

I am having a problem of which I get a syntax error (Unexpected else)
Below is my code. I think it would be a "curly bracket" or semi-colon problem
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 swins";
} else {
return "paper wins";
}
} else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
} else if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins";
} else {
return "scissors wins"
}
}
};
Any help would be appreciated. Thanks

Please place below code, I added one missing close bracket for last else part else if (choice1 === "paper").
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock swins";
} else {
return "paper wins";
}
} else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
} else if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins";
} else {
return "scissors wins"
}
}
}
};
Let me know if there is any error remain.

Related

Why is this javascript function call not working?

Calling my function yields a syntax error, everything else works on its own. The error is thrown on.
console.log(compare(userChoice, computerChoice));
The error is:
SyntaxError: Unexpected token {
Any ideas?
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";
}
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 (choice1 === "scissors")
{
if (choice2 === "paper"
{
return "paper wins"
}
else
{
return "scissors wins"
}
}
}
console.log(compare(userChoice, computerChoice));
Yes, you forgot the ) at if (choice2 === "paper".
As others have pointed out, one of your closing )s was missing.
With lots of nested if-else blocks, it's easy to lose track of your ()s and {}s. You can write much shorter, nicer, easier to read code if you utilise other conditional statements and expressions, namely the switch statement and the ? operator:
function compare(choice1, choice2) {
if (choice1 === choice2) {
return 'The result is a tie';
}
switch(choice1) {
case 'rock':
return choice2 === 'scissors' ? 'rock wins' : 'paper wins';
case 'paper':
return choice2 === 'rock' ? 'paper wins' : 'scissors win';
case 'scissors':
return choice2 === 'paper' ? 'scissors win' : 'rock wins';
}
}
As you can see, this code has much fewer ()s and {}s.

Another Rock, Paper, Scissors

I'm just trying to build the first two steps of my r/p/s game, and here is my coding so far. It is giving me the response "Syntax error: unexpected token else" and I cannot for the life of me figure out why...
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";
}
}
You closed the main if/else chain with your paper wins section, I believe you meant to attach that else to the inner if/else as below.
In the paper section you had the same thing, I fixed that instance as well. From here you should be able to add the final if(choice1 === "scissors") segment.
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";
}
}
I fixed this for you:
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";
}
}
}
You've got a muddle of ifs and elses!
I believe specifically, you've got if (choice2 === "scissors") without curly braces, which is fine, except then you closed the brackets you never opened with an } else {. You should indent properly, it will help you see why you might have run into an issue.
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";
}
}
}
So you can see, I added { after any 'ifs' without, and then added two } to close those in the correct spots.
else {
return "Paper wins";
}
else if (choice1 === "paper") {
you cannot do a else if after doing a else;
I was negated by not giving the full answer and force him to study conditions?

JavaScript SyntaxError: Unexpected token else

I have this code for a rock, paper, scissors game that is basically homework. I have double-checked and it seems to be fine, however, when I run it it says:
SyntaxError: Unexpected token else,
any help will be very appreciated :) Please note that I am a newbie, so if the question is dumb, please be nice and help <3
I just edited the code a bit, since I had many "Overlook" mistakes. I also wanted to clarify that I need all the code located after the function statement to be inside the function, that is why I don't close the first { right away.
PD: Now I get: SyntaxError: Unexpected token =
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 if (choice1 ==== "paper") {
if (choice2 === "rock") {
return "paper wins";
else if (choice2 === "scissors") {
return "scissors wins"; }
else {
return "Paper wins"; }
}
}
}
compare(userChoice, computerChoice)
Ok, to stay true to your homework, I kept the same format just fixed the issues.
here it is:
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!";
}
if (choice1 === "paper") {
if (choice2 === "rock") {
return "Paper wins!";
} else {
return "Paper looses!";
}
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "Rock wins!";
} else {
return "Rock looses!";
}
}
if (choice1 === "scissors") {
if (choice2 === "paper") {
return "Scissors wins!";
} else {
return "Scissors looses!";
}
}
}
compare(userChoice, computerChoice)
Consider re-writing it in a little easier way.
See fiddle https://jsfiddle.net/DIRTY_SMITH/c7ww2hmz/1/
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";
}
alert("the computer picked " + computerChoice);
if ((computerChoice === "rock" && userChoice === "papper") || (computerChoice === "papper" && userChoice === "scissors") || (computerChoice === "scissors" && userChoice === "rock")) {
alert("you won");
} else if (computerChoice == userChoice) {
alert("It's a tie");
} else {
alert("you loose");
}
You will find your debugging much easier if you
properly indent,
use braces for any but the simplest if statements
For example:
if (choice1 == choice2) return "tie"; /* simple 1-line if is ok */
if (choice1 == "rock") {
if (choice2 == "scissors") { /* more complex, always use braces */
return "rock wins"; /* always indent nicely */
} else {
return "paper wins";
}
}
/* ... and so on ... */
Always properly format your code. You are missing a bunch of } before the else statements. Always use semi-colons at the end of a line (no, you don't technically need to be it is extremely good practice).
Also, you need to watch your equals. You had one ==== instead of ===
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 if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else if {
return "paper wins";
} else {
return "Paper wins";
}
}
}
}
compare(userChoice, computerChoice)

SyntaxError: Unexpected token if

I'm currently learning javascript and I keep having this error!!!
This is my script:
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";
}
Should be:
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";
}
Or neater:
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"
}
}

Javascript game giving wrong output

I just completed a few exercises on "CodeAcademy" which taught me to create a rock,paper and scissors game. Everything works fine except the output that I want to receive. I am not getting the out "paper wins". All i get it "paper". I have just started to grasp the foundation of Javascript which is why I am not a strong scripter yet.
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random(0);
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!");
}
if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
if (choice2 === "scissors") {
return "scissors wins";
}
}
if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins";
} else {
if (choice2 === "paper") {
return "scissors wins";
}
}
}
}
};
I think you are invoking the compare function on selection of the choices. so you are sending user choice to the function. now you should compare it with computerChoice. what is choice2 there?
As others have stated you never call your compare function and you never return any value of that function. And even if you did your function would fail most of the time anyway.
You structure:
if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
if (choice2 === "scissors") {
return "scissors wins";
}
}
if (choice1 === "scissors") { //is never true because choice1 is always "paper"
if (choice2 === "rock") {
return "rock wins";
} else {
if (choice2 === "paper") {
return "scissors wins";
}
}
}
} //if choice1 === "paper" ends here
//no love for the rock?
What you want to achieve:
if (choice1 === "paper") {
if (choice2 === "rock")
{
return "paper wins";
}
else if (choice2 === "scissors")
{
return "scissors wins";
}
}
else if (choice1 === "scissors")
{
if (choice2 === "rock")
{
return "rock wins";
}
else if (choice2 === "paper")
{
return "scissors wins";
}
}
else if (choice1 === "rock") //don't forget the rock
{
if (choice2 === "paper")
{
return "paper wins";
}
else if (choice2 === "scissors")
{
return "rock wins";
}
}
Then you will have to call your function like this:
var result = compare(userChoice, computerChoice);
And you log the result variable. In your example you logged computerChoice which can only be "paper", "scissors" or "rock" because that is the value you give it, not the value of the function.
Since you let the user type in anything he/she wants you will obviously also have to validate their input or the if/else structure will fail.

Categories

Resources