Lesson 8. Maybe there's a bug


#1



Whatever I choose I always get the same error message, 'Oops, try again. Your code returned 'Paper wins' instead of 'undefined' when the inputs are scissors and rock' I don't know why and I have tried different code and suggestions from other topics but none work.


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


#2

What should your function return when input is 'scissors', 'rock'? (or in reverse order)
What does your function return for that input?


#3

Like the thing in the black box? (The code box thing on the right)


#4

I'm afraid you'll have to use a few more words if you want to get your message across.


#5

The error message that codecademy gives you doesn't make sense. Your function shouldn't return undefined.
However, it mentions some input, so you can at least test your function for that input.


#6

No matter what I type in the prompt box or what the computer choices the error message makes no sense and is quite confusing, the error message is still https://gyazo.com/f3ce405c7e1a69248f06bf424f464bce
(I took a screenshot, by the way I said rock in that one)


#7

Have you made any attempt at testing your function with the input that the error message mentions?

What does a function do, what behaviour is your function supposed to have? How is it supposed to be used?

A function is invoked with arguments, and it returns a result.
So to test it, invoke your function with the arguments that the error message mentioned. Inspect the result.
Right?


#8

I did the return all wrong, lol. I was doing return ('example'); when it should have been return 'example', here's the new code.

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

#9

Im completely lost and my code is the following above and it still showing as a syntax error but also im quite confused on the compound if/else statements and were wondering if anyone could clarify my misunderstandings , Thanks :

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


#10

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