8. What if choice1 is scissors?


#1



Everything works fine, but no matter what choice I type in, the error code says, "
Oops, try again. Your code returned 'Paper wins!' instead of 'undefined' when the inputs are scissors and rock"

Here is my code:


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 === "paper") {
            return "Scissors win!"
        }
        else {
            return "Paper wins!"
        }
    }
};

compare(userChoice, computerChoice)


#2

here:

    else if (choice1 === "scissors") {
        if (choice2 === "paper") {
            return "Scissors win!"
        }
        else {
            return "Paper wins!"
        }
    }

when else will run, this means choice1 is scissors, and choice2 is rock, and then paper wins?


#3

What do you mean? What I am trying to say is is there something wrong with my code? Is there something that is not printing or something not running properly? And if so, why?


#4

lets say you are choice1 and you choice scissors and i am choice2 and i pick rock

according to your code, paper wins them.... i should win since i picked rock


#5

The error checking in this exercise is odd and confused me too. It will produce the error "oops you returned '___' instead of 'undefined'" if your code isn't what the exercise is expecting. I actually had to rewrite and check mine a couple of times.

As stetim94 explained, in that branch you have it so if the user picks scissors and the computer picks paper, the user's "scissors" wins. Your else condition however, which would be when the computer picks "rock", should be "rock wins". Paper isn't in that condition (the player picked scissors and the computer picked rock).

The else by itself makes this a little confusing. To help organize your code, you can put a comment in the "else" line explaining what condition that is. In this case, you could put else { //computer picks rock to help you remember


#6

this is indeed confusing, the error message is actually bugged. udefined should be rock wins.


#7

Ok. What do I need to switch around specifically? Just the error in the branch or is there more errors that come out in this branch?


#8

really?

of this section of code:

    else if (choice1 === "scissors") {
        if (choice2 === "paper") {
            return "Scissors win!"
        }
        else {
            return "Paper wins!"
        }
    }

this specific line:

return "Paper wins!"

returns the wrong thing. if you get to this branch, choice1 is scissors and choice2 is rock, how on earth can paper win then?


#9

I'm so stupid... sorry for being a bit of a burden. I was a little confused on what was going on. I can't believe on how I didn't see that. I guess I am more accustomed to python as it is more specific in telling me on what I did wrong. But most of the time in Java it just says "Unexpected token" or, "Undefined" Instead of telling me what line was wrong or what command was invalid. I guess this is good as it will train me to see where the code is going and to not rely off of the error codes as much. Again, sorry for being such a burden. (Plus thanks for the help)


#10

no problem, just kind of confused you didn't see that.

Yea, i find the python interpreter giving nicer error message. i sometimes use jsbin (open the javascript and console tab) it underlines if there are syntax problems somewhere. There are plenty of other js debuggers out there, must be.


#12

I get that part but the last part: call the compare functions "userChoice" and "computerChoice" as the two arguments. How on earth do I do that?


#13

look at this example:

# function declaration
var example(a, b):
   return a + b

# function call with arguments
example(5,4)

#14

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