Error Checking - Undefined : Bug - Lesson 8


#1

On 8 I get error message "Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and paper"

If i change the return to "undefined" i successful pass the lesson:)


#2

I have the same issue... I've tried changing both 'paper win' statements to 'undefined' to pass but but revoked anyway... don't know how to pass this one..


#3

Well to give hints one would need to see your code. But concerning the error message I'd guess that you should examine the case compare("scissors","paper") further as this seems to be the case where unexpected things happen. But undefined is not the solution.


#4

// FAILS WITH ERROR : 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";
} 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";
        }
    }
    else if (choice1 === "scissors")
     {
        if (choice2 === "rock")
        {
            return "rock wins";
        }
        else 
        {
            return "paper wins";
        }
    }
};

compare(userChoice, computerChoice);

// PASSES THE CLASS : Changed last else statement from "return 'paper wins'" to "return 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!";
    }
    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 undefined;
        }
    }
};

compare(userChoice, computerChoice);

#5

I can't pass this lesson without putting "undefined" in place of "scissors wins," and I'm not sure what is going wrong.
With this code I get the error "Oops, try again. Your code returned 'scissors wins' instead of 'undefined' when the inputs are scissors and rock.":

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

But this code works:

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

I could use some help figuring out what is going wrong. Thanks!


#6

@lgnd aid for an input of scissors vs paper you get an output of paper wins. Obviously this is incorrect and returning undefined might fool the test function, worse enough, but it is clearly not the result you're looking for, is it? What about just using the logical choice of returning scissors wins?

@arrayrunner83794 Seems like your else if cases aren't quite right:

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

here the else should belong to the if not to the else if. Have a look at the hint of 6/9 to see a working example.


#7

I finally got this to work by explicitly defining all the "choice2" options (as below), but shouldn't it work by simply using "else" rather than needing to define each choice2?

var compare = function(choice1, choice2) {
if(choice1 === choice2) {
    return "The result is a tie!";
} else 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";
    }
    if(choice1 === "rock"){
        if(choice2 === "paper")
        return "paper wins";
    } else if(choice2 === "scissors") {
        return "rock wins";
    }

 }   
 compare (userChoice, computerChoice)

#8

That's not what I meant.
Take a look at this case:

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

and go through the relevant cases (tie case excluded that is save). What would be the result of rock vs scissors? Well it should be rock but let's see:

Ok it choice1 is rock which is not paper so you go on with the else if. Now choice2 is scissors so you pick this one and return scissors wins. That is not really what is meant to be returned, as said have a look at the hint of 6/9 to see a working example.


#9

Thank haxor789.
I guess i was just debugging by referring to the error messages rather than walking through the code. Changing the undefined to "Scissors wins" passes.


#10

@ Hazor789

Where is the 6/9 hint? Is that the date?


#11

The hint in the exercise 6th exercise (that one about rock I guess).