8. What if choice1 is scissors?


#1



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


I get various error codes depending on what I input:

If I input rock, get:
Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and paper

If I input scissors, get:
Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and paper

If I input paper, get:
Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and paper


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


#2

@scottm14172 That's a funny error, it makes it sound like the checker wants you to return undefined hehehe :grin:

However it seems weird that your program returns for example, paper wins when you input scissors, no?

The part of your code that you shared seems to work as expected for me when I run it inside my program; maybe your computerChoice section doesn't align with your compare function, etc...? Can you please share the whole program?

frisby


#3

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

}

#4

@scottm14172 Thanks! Did you perhaps miss the function call in your copy and paste? Or maybe you haven't written it yet?


#5

I guess I haven't, because it is all copied


#6

Ah I see, just checking. :slight_smile:

I believe the instructions for this exercise ask you to call the function (otherwise no choices will be compared). Also, there don't seem to be any conditions written for when choice1 === "paper". What will happen when the user chooses paper?*

Can you try addressing these two points and then see if it works?

*Since we are supposed to write the scissors section in this lesson maybe you just accidentally deleted it and can copy it over from the previous lesson? :wink:


#7

Since they are building the program in steps I tried to follow steps.(some done previous parts, I assume more to come)

No step for what if choice1 is paper, ■■■■ the title of this section is what is choice1 is scissors.

Not well written instructions i guess.


#8

how am i supposed to call it, not sure i understand that


#9

@scottm14172 That's strange, I had it on my end, here is the direct link to the paper section.


#10

@scottm14172 From the function lesson:

To use the function, we call the function by just typing the function's name, and putting a parameter value inside parentheses after it. The computer will run the reusable code with the specific parameter value substituted into the code.

@rcodeman does a great writeup on function call basics, a copy of which can be found here.

If you still have trouble understanding feel free to ask any follow up questions :slight_smile:


#11

Well that is strange somehow I got by with putting choice1 on Step7 as rock instead of paper and it let me pass that section. I was looking at the area above that tries to teach you and not as closely at the instructions. they really need to do a better job on something. like they have made changes but didn't catch all the little things


#12

That is strange. Yeap, sometimes the checker passes things it shouldn't and occasionally "fails" things it should. Were you able to resolve the issue?


#13

I am gonna go back 3 steps and totally start again. too fed up as it is.
not learning anything by trying to just fix it just to move on.


#14

That's true, though debugging can be a learning experience, too. Hope the lesson goes more smoothly for you the second time round :slight_smile:


#15

Think I am there now, but is the call

compare(userChoice,computerChoice);


#16

@scottm14172 Looks good to me :heavy_check_mark: