8 What if choice is scissors


#1

It's gotta be something small. I am losing my mind



Replace this line with your code.

https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/4?curriculum_id=506324b3a7dffd00020bf661


#2

It would be easier to determine issues if you copy and pasted your actual code and posted it. From the screenshot, I see you have semicolons on lines 28, 36 and 44. There may be other snags though.


#3

The thing is my code worked up until this last lesson. That's what I dont get

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(userChoice, computerChoice) {
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 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") {
if (choice2 === "paper") {
return "paper wins"
}
else if (choice2 === "scissors") {
return "rock wins"
}
};


#4

Got rid of the semicolons. i had added them to see if that was the problem. It wasn't. No they are gone and I am getting the error unexpected token else

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(userChoice, computerChoice) {
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 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") {
if (choice2 === "paper") {
return "paper wins"
}
else if (choice2 === "scissors") {
return "rock wins"
}
}


#5

The very last closing curly bracket here ends your compare function prematurely.

I can see what you're trying to do here with your nested if/else statement. The instructions suggest using this format:

else if (choice1 condition){
if (choice2 condition){
return winner code;
}else{
return winner code;
}
}


#6

I think I just mixed things up too much by cutting things out and adding other things in. I deleted the last if / if else /else statements and it passed me. I think I learned the right way. I hope


#7

Post it, maybe I'll learn something from you :slight_smile:


#8

I had it all right up until that last lesson so I just deleted the stuff I added. It left out a few responses but the rest was just like the code here so..... Anyway it past me. I would kind of liked to know what I did wrong but I'm sure it was just a semicolon or a bracket in the wrong place.

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 {
return "Paper wins";
}
}

else if(choice1 === "paper"){
  if(choice2 == "rock"){
     return "paper wins" 

  }else{
       return "scissors wins"
  }
}

}


#9

Looks good except you don't have an else if for choice1 === scissors...


#10

I don't know how stuff got cut out in the middle of it but it did. All I know is that I have had no problems or questions until that exercise and I got very frustrated. I was sure I had it all right and neat and then it wouldn't work.


#11

It's a learning process. Some days I think I got it and some days I'm back to square one.


#12

It helps that I learned Python first. It's very similar except for all the curly brackets and such.


#13

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