Rock Paper Scissors Part 8


#1

Apparently this is enough to me to pass this part but something is clearly wrong. Whenever I pick scissors and the computer uses rock I get undefined. I tried correcting the scissors part but I kept getting syntax errors so could someone point out what my code is missing? I removed the part that was filled with syntax errors so this code works but it's still flawed.

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";
}
}
}
console.log(compare(userChoice, computerChoice));


#2

I'm unsure on something. All the other options that are chosen return correctly?


#3

The compare function already returns the values on their own to the console, by passing it through console.log, you're obfuscating the value.


#4

I was thinking that too, I don't believe you can put a function into another function as he is attempting


#5

I was going to mention that too, but i'm not sure if that's always the case. Figured what I said was enough : )


#6

Where did he put a function within a function? I see a function with 2 parameters printed out by an object. ---

~console.log(**compare(---userChoice, ---computerChoice));

~ 'object'
** 'function'
--- 'parameters'

Where are there two functions?


#7

Yes if you'd like to be technical, console is an object and log is a method.
Though a method is a function with a specific scope, it's really irrelevant here. I'm not really big on unloading a great deal of information when there is already a problem to fix. It's bad for the me, and bad for them. It's best to take things step by step.

Here's a quick clarification:


#8

Thank you!

I was also doing this exercise but my problem was different. I think the exercise program had trouble reading my code although I think I had the right syntax within my compare function.

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

The program tells me I have a syntax error '{'


#9

A syntax error generally means that "flow" of your code is interrupted somewhere. The error I received was 'unexpected else' which means that one of your else statements is surprising the browser, you could be missing a closing curly bracket.

From what I see here, lining up the rest of your statements, you're introducing an extra closing curly bracket


#10

Oh, shoot! Okay thank you. I just made the correction. One more error came up and I hope I'm not taking too much of your time asking you for one last help!

SyntaxError: Unexpected token else <------ This shows when I run it.

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


#11

Look through each line and indentation to make sure the number of statements and their closing brackets match the number of statements on that indentation.

For example there is no closing bracket on the same indentation for your last else if statement.


#13

Sure it does but if you remove the compare function in the console.log
instead of getting the return value you get both the user choice and the
computer choice which isn't the desired result.


#14

I figured out the issue. I just made another nested else if function for scissors and it fixed everything up. I'll put the part I made in bold to show what I changed because nested functions are pretty confusing looking.

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";
}
}
}
console.log(compare(userChoice, computerChoice));