What if choice1 is paper? help

#1

Hi! Before this activity, I was having trouble with 'What is choice1 is rock?', until I received help. Now this one isn't working. Can you please help me? Thanks

else if(choice1 === "paper") {

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

#2

Hello there @arquerkii! Could you please post all of your coding so I can analyse it for mistakes / disagreements? I don't see the error with the small sniplet you provided...

-SilasMao

#3

Here you go @silasmao

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 === choice2) {
return "The result is a tie!";
}
else if(choice1 === "paper") {

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

#4

Hello again @arquerkii , before I check it further, notice that you have

if (choice1 === choice2)

written twice in your code. No matter if choice 1 and choice 2 are any of the three options, using the statement once is enough. think of it as basic algebra.

if we know that the choices are equal, it doesn't really matter which of the choices were actually chosen. So, you shouldn't need to repeat that statement for each of the choices. It's not parsimonious.

Try to remove that and see if it works. If not, please come back and post what you've got after you tried to fix it and I'll check it out from there.

#5

Still not working...

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";
}
}

#6

Thanks for doing that work first @arquerkii . I like to help people in a stepwise way so it's a bit more clear, haha.

Yep, okay, so first, try to be consistent and orderly with your tabbing/indenting of codes as it will be an invaluable help while you program. I cannot count the number of times I've caught a mistake because i could easily catch brace mistakes. Here, you had a brace issue in your code that was throwing things off!

Look at the following code that worked, and really take the time out to notice the differences and where yours deviated from it, especially in brace position.

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";
}
}

}

Please let me know if you need any other help!

#7

Hey @silasmao, I need help with the next one:
Not sure how to 'Call your function and pass in userChoice and computerChoice as your two arguments'

Here's 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 wins";
}
else {
return "rock wins";
}
}
var function = (userChoice, computerChoice)

#8

@arquerkii, there are two things I've noticed so far with this next question.

1) There is a brace issue again. This time, after the else if (choice 1 === "paper") you end with three braces, when in fact you should have two. That third brace belongs AFTER the else if (choice 1 ==="scissors") statement. So try to have your braces in the following set-up. Your formatting looks much better this time, just be careful to know where braces go so you can easily see if they're closing the right part of code. For example, I like to have the second/end brace line up UNDER the element that it is paired to. Example:

var squid = function (tentacles) {
//code here!
}

By placing the } under the v in var, I know that it is responsible for matching/closing to the function I made, and not anything within the coding inside the function.

So, after fixing your brace mistake you should have:

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

}
var function = (userChoice, computerChoice)

2) Once we have that, we need to understand the basic way we call (fancy word for "use") a function. It is as follows:

functionName(parameter1,parameter2);

Note that what you've done is:

var function(parameter1,parameter2);

Why is that wrong? Let's see:
var declares a new variable. You are right in that you need to be calling a function. However, var is used not when calling upon the function but instead when you're assigning the function's code to be stored in a certain word (var word = function (param1,param2) {//code here;}). You've already created a function to do what you want, so you don't need to create another one! The function of interest is "compare" -- it's at the top of your code.

Compare the basic function making syntax ...

var word = function (param1,param2) {
//code here;
}

to the first line of your code:

var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
}

See the similiarities? So, that is a bit of background on how to make/declare a function. You've already got your function made, though, so what you need to do is figure out how to take variables (choice1, choice2) and make your function USE those variables and churn out some result.

The syntax we use to call/use a function like that is:

functionName(param1,param2);

with your code in this exercise, it'd look something like

compare(userChoice,computerChoice);

I rambled a bit, but wanted to make sure I covered anything that might be confusing you. Let me know if you understood everything I said pretty well-- or if not.

Another test you could do to make sure you understand how this is working is to ask yourself why "userChoice" and "computerChoice" were used as parameter1/parameter2 instead of "choice1" and "choice2". If you need help with that, I can answer this for you as well. Try to think about it on your own for a minute or two.

Hopefully that helped!

-SilasMao

(P.S. I'll be heading to sleep now, but will check back in with you in about 8-10 hours. Keep at it! You've got this! )

#9

Ok I think I understand it, I'll get back to you on making it work!

#10

Hey @silasmao,
For some reason, when I used your code, it didn't work. SO I tried looking up other people's problems, and finally found one that worked:

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 "scissors wins";
}
}
};

compare(userChoice, computerChoice);

I don't know why yours didn't work, but thanks for trying. I understand what you were saying, and I jotted down your exemplars.

#11

Oops, that's a bit embarrassing. Sorry for potentially confusing you. Just following up to clarify WHY my code didn't work --> I forgot to change the part of your code that was wrong, after writing so much to describe what the problems were.

Namely, notice that:

1. The way there is 3 braces after the second-to-last else if statement (beginning with else if (choice1==="paper")...), that CLOSES the function's coding, which is suppose to include all of these else/else if/if statements.

Then, the computer gets confused because you have ANOTHER else if (choice1==="scissors").... statement that isn't included in the function! If we wanted another loop to occur outside of the function entirely, it'd of course have to start with if (choice1==="paper"). But that's of no use to our coding needs.

1. Then of course you'd have to switch the function call formatting to proper formatting where functionName(param1,param2); is the general format.

Sorry to have confused you by never posting the completely editted version. ^^; Hope you're not discouraged.

-SilasMao

#12

Everyone makes mistakes. I'm not discouraged, don't worry about it. @silasmao

#13

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