Code not being excepted, not sure what it looking for. the code works


#1


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


This code works but isn't being excepted.
Error Message:
Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and paper


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!";
    };
    
    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(choice2 === "rock"){
            return "rock wins";
        }else{
            return "paper wins";
        }
    };
};

console.log(userChoice);
console.log(computerChoice);

compare(userChoice,computerChoice);


#2

Try removing these two and running it again,


#3

Removed those 2 lines.
Still the same error.


#4

I am also getting the same error. But code works fine and o/p is correct


#5

Your last return line should refer to scissors winning.

For the final part of your code, If choice1 is scissors, which is never explicitly stated, but implied in the 2nd to last else statement in your code, then:
- when choice2 is a scissors, there is a tie (which is at the top of the code);
- when choice2 is rock, rock wins
- when choice2 is neither rock or scissors (i.e., it is paper), then scissor wins.


#6

Another way to look at my earlier explanations is that the if and the else statement have the same return lines:
- first return line, rock wins; and
- second return line, paper wins.

Rock, paper, and scissors should each win 1/3 of the time.
Under your scenario paper wins 3 times, rock wins 2 times, and scissor only wins 1 time.

I hope this helps.


#7

Remove the semicolons ( ; ) after the grouping symbols ( { } )


#8

Had the same problem. The game was running correctly but was having the same error.
On the instructions they give you two options for the if/else scissors. I was using option b. Once I change it for option a. I was allowed to continue.

This is my original code with the 'problem' that didn't let me pass:

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) ;
console.log("User: " + userChoice) ;

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 "scissors wins" ;
        }
        else {
            return "rock wins" ; 
        }
    }
} ;

compare(userChoice,computerChoice) ;

here is the 'fixed' code that allowed me to pass to the next exercise:

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) ;
console.log("User: " + userChoice) ;

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 === "rock" ) {
            return "rock wins" ;
        }
        else {
            return "scissors wins" ; 
        }
    }
} ;

compare(userChoice,computerChoice) ;

hope this helps.

NOTE: This is my first post and I apologise if something is not done as it should be. Please ping me if that's the case to help me improve for the next time.


#9

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