Lesson 8, Bugged?


#1

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

It says it gave me "paper wins" instead of "undefined"


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


#2

It seems that you have completely neglected scissors like what happens if choice 1 is scissors but choice two is rock? what about when scissors is choice 1 but paper is choice 2? I would suggest going back and adding those in as else if and if statements with returns


#3

I'm confused sir, can you post the fixed code and explain it?
I'm brand new to javascript.


#4

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

This is what I have now, and it's still not working, I also ran it trhough JSHint and it said i have an "unmatched {"


#6

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 === "rock"){
      if (choice2 === "scissors"){
         return "rock wins";
      } else {
         return "paper wins";
      }
   } // missed } here
   else if(choice1 === "paper"){
      if (choice2 === "rock"){
         return "paper wins";
      } else {
         return "scissors wins";
      }
   } // missed } here
   else if(choice1 === "scissors"){
      if (choice2 === "paper"){
         return "scissors wins";
      } else {
         return "rock wins";
      }
    } // missed } here
}
console.log(userChoice);
console.log(computerChoice);

// call the function
console.log( compare(userChoice, computerChoice) );

#7

Thank you! You should be a mod here!


#8

You're welcome! :slight_smile:


#9

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