Why is this javascript function call not working? - javascript

Calling my function yields a syntax error, everything else works on its own. The error is thrown on.
console.log(compare(userChoice, computerChoice));
The error is:
SyntaxError: Unexpected token {
Any ideas?
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";
}
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 "paper wins"
}
else
{
return "scissors wins"
}
}
}
console.log(compare(userChoice, computerChoice));

Yes, you forgot the ) at if (choice2 === "paper".

As others have pointed out, one of your closing )s was missing.
With lots of nested if-else blocks, it's easy to lose track of your ()s and {}s. You can write much shorter, nicer, easier to read code if you utilise other conditional statements and expressions, namely the switch statement and the ? operator:
function compare(choice1, choice2) {
if (choice1 === choice2) {
return 'The result is a tie';
}
switch(choice1) {
case 'rock':
return choice2 === 'scissors' ? 'rock wins' : 'paper wins';
case 'paper':
return choice2 === 'rock' ? 'paper wins' : 'scissors win';
case 'scissors':
return choice2 === 'paper' ? 'scissors win' : 'rock wins';
}
}
As you can see, this code has much fewer ()s and {}s.

Related

JavaScript SyntaxError: Unexpected token else

I have this code for a rock, paper, scissors game that is basically homework. I have double-checked and it seems to be fine, however, when I run it it says:
SyntaxError: Unexpected token else,
any help will be very appreciated :) Please note that I am a newbie, so if the question is dumb, please be nice and help <3
I just edited the code a bit, since I had many "Overlook" mistakes. I also wanted to clarify that I need all the code located after the function statement to be inside the function, that is why I don't close the first { right away.
PD: Now I get: SyntaxError: Unexpected token =
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 if (choice1 ==== "paper") {
if (choice2 === "rock") {
return "paper wins";
else if (choice2 === "scissors") {
return "scissors wins"; }
else {
return "Paper wins"; }
}
}
}
compare(userChoice, computerChoice)
Ok, to stay true to your homework, I kept the same format just fixed the issues.
here it is:
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!";
}
if (choice1 === "paper") {
if (choice2 === "rock") {
return "Paper wins!";
} else {
return "Paper looses!";
}
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "Rock wins!";
} else {
return "Rock looses!";
}
}
if (choice1 === "scissors") {
if (choice2 === "paper") {
return "Scissors wins!";
} else {
return "Scissors looses!";
}
}
}
compare(userChoice, computerChoice)
Consider re-writing it in a little easier way.
See fiddle https://jsfiddle.net/DIRTY_SMITH/c7ww2hmz/1/
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";
}
alert("the computer picked " + computerChoice);
if ((computerChoice === "rock" && userChoice === "papper") || (computerChoice === "papper" && userChoice === "scissors") || (computerChoice === "scissors" && userChoice === "rock")) {
alert("you won");
} else if (computerChoice == userChoice) {
alert("It's a tie");
} else {
alert("you loose");
}
You will find your debugging much easier if you
properly indent,
use braces for any but the simplest if statements
For example:
if (choice1 == choice2) return "tie"; /* simple 1-line if is ok */
if (choice1 == "rock") {
if (choice2 == "scissors") { /* more complex, always use braces */
return "rock wins"; /* always indent nicely */
} else {
return "paper wins";
}
}
/* ... and so on ... */
Always properly format your code. You are missing a bunch of } before the else statements. Always use semi-colons at the end of a line (no, you don't technically need to be it is extremely good practice).
Also, you need to watch your equals. You had one ==== instead of ===
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 if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else if {
return "paper wins";
} else {
return "Paper wins";
}
}
}
}
compare(userChoice, computerChoice)

Javascript game giving wrong output

I just completed a few exercises on "CodeAcademy" which taught me to create a rock,paper and scissors game. Everything works fine except the output that I want to receive. I am not getting the out "paper wins". All i get it "paper". I have just started to grasp the foundation of Javascript which is why I am not a strong scripter yet.
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random(0);
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!");
}
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";
}
}
}
}
};
I think you are invoking the compare function on selection of the choices. so you are sending user choice to the function. now you should compare it with computerChoice. what is choice2 there?
As others have stated you never call your compare function and you never return any value of that function. And even if you did your function would fail most of the time anyway.
You structure:
if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
if (choice2 === "scissors") {
return "scissors wins";
}
}
if (choice1 === "scissors") { //is never true because choice1 is always "paper"
if (choice2 === "rock") {
return "rock wins";
} else {
if (choice2 === "paper") {
return "scissors wins";
}
}
}
} //if choice1 === "paper" ends here
//no love for the rock?
What you want to achieve:
if (choice1 === "paper") {
if (choice2 === "rock")
{
return "paper wins";
}
else if (choice2 === "scissors")
{
return "scissors wins";
}
}
else if (choice1 === "scissors")
{
if (choice2 === "rock")
{
return "rock wins";
}
else if (choice2 === "paper")
{
return "scissors wins";
}
}
else if (choice1 === "rock") //don't forget the rock
{
if (choice2 === "paper")
{
return "paper wins";
}
else if (choice2 === "scissors")
{
return "rock wins";
}
}
Then you will have to call your function like this:
var result = compare(userChoice, computerChoice);
And you log the result variable. In your example you logged computerChoice which can only be "paper", "scissors" or "rock" because that is the value you give it, not the value of the function.
Since you let the user type in anything he/she wants you will obviously also have to validate their input or the if/else structure will fail.

Rock,Paper,Scissors Game on Codecademy

I tried to incorporate the additional suggestions given on the website(Codecademy), viz. the feature in which there's a tie and the provision for taking input again. This is the error-containing piece of code:
var userChoice, computerChoice;
var choicesDetermination = function(){
userChoice = prompt("Do you choose rock, paper or scissors?");
if(userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors"){
userChoice = prompt("Invalid input. Please try again.\n Do you choose rock, paper or scissors?");
}
computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log("User: " + userChoice);
console.log("Computer: " + computerChoice);
}
var compare = function(choice1, choice2){
if(choice1 === choice2){
console.log("The result is a tie! Let's try one more time.");
choicesDetermination();
compare(userChoice, computerChoice);
}
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 "rocks wins";
}
}
}
}
choicesDetermination();
compare(userChoice, computerChoice);
Here, choicesDetermination() is the function in which I've taken the input and stored them in userChoice, computerChoice(both global variables).
I don't know why but the code seems to run fine when I ask for the input again; the variables are changed correctly. But the function compare() doesn't run correctly; the return statements don't get printed to screen.
What I've always found with Code Academy is that the output strings have to be annoyingly perfect, so check that first!
I think the issue with your code is that you're actually supposed to return "The result is a tie! Let's try one more time." not console.log.
Please find my complete, passing code below:
var userChoice = prompt("Do you choose rock, paper or scissors?");
if (userChoice != "rock" && "scissors" && "paper") {
alert("Please enter 'rock', 'scissors', or 'paper' as shown.");
userChoice = prompt("Type carefully, please: 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 (choice2 === "rock") {
return "rock wins";
}
else {
return "scissors wins";
}
}
};
compare(userChoice, computerChoice);

Rock, Paper, Scissors in JavaScript

I'm on a codecademy lesson (found here) but keeps telling me "Your code returned 'rock wins' instead of 'paper wins' when the inputs are paper and rock", why? It should be correct. Since it's talking about 'rock wins' then it's talking about rock vs scissors. So why is it saying that "instead of paper wins" when there isn't even paper involved in the only outcome of 'rock wins'?
var compare = function (choice1, choice2) {
if (choice1 === choice2) {
return("The result is a tie!");
}
if (choice1 === "rock") {
if (choice2 === "scissors");
} else {
return ("rock wins");
}
if (choice1 === "paper") {
if (choice2 === "rock");
} else {
return ("paper wins");
}
if (choice1 === "paper") {
if (choice2 === "scissors");
} else {
return ("scissors wins");
}
};
Look at your first condition:
if (choice1 === "rock") {
if (choice2 === "scissors");
} else {
return ("rock wins");
}
So if choice1 is rock, you enter the if-block (which doesn't actually return anything, but since in this case choice1 is actually "paper" it goes into the else-block, which unconditionally returns "rock wins". Try refactoring it to be something like this:
if (choice1 === choice2) {
return("The result is a tie!");
}
if (choice1 === "rock") {
if (choice2 === "scissors") {
return ("rock wins");
} else {
return ("paper wins");
}
}
if (choice1 === "paper") {
if (choice2 === "rock") {
return ("paper wins");
} else {
return ("scissors wins");
}
}
if (choice1 === "paper") {
if (choice2 === "scissors") {
return ("scissors wins");
} else {
return ("rock wins");
}
}
But hey, let's get fancy. Try putting your choices into an array:
var choices = ["rock", "paper", "scissors"];
Now, notice that the item to the right always beats the item to the left (if we consider that the array wraps around). How can we use this to simplify the code? Well we can just compare the indexes of each choice, taking care to handle the edge case of scissors vs. rock:
var x = choices.indexOf(choice1),
y = choices.indexOf(choice2);
if (x === y) {
return("The result is a tie!");
} else if (x > y) {
if (x == 3 && y == 0) {
return choice2 + " wins";
} else {
return choice1 + " wins";
}
} else {
return choice2 + " wins";
}
But we can use the remainder operator (%) here to handle the edge case more easily:
var choices = ["rock", "paper", "scissors"];
var compare = function (choice1, choice2) {
var x = choices.indexOf(choice1),
y = choices.indexOf(choice2);
if (x === y) {
return("The result is a tie!");
}
return (((x - y) % 3) > 0 ? choice1 : choice2) + " wins";
}
Your function will always return "rock wins" when choice1 is not "rock". This is because you have used the if - else statement.
What you are doing is:
if choice1 is rock do something
else return "rock wins"
I will give you the first statement:
if (choice1 === "rock") {
if (choice2 === "scissors") return ("rock wins");
if (choice2 === "paper") return ("Paper wins");
}
jsFiddle Demo
There are interesting choices you made when using the if statements. There shouldn't be semi colons after them. Also, the logic composition can struggle when using many if then else statements. Often, it is best to use a switch case statementMDN in these situations.
var compare = function (choice1, choice2) {
if(choice1==choice2)return "The result is a tie!";
switch(choice1+choice2){
case "rockscissors": case "scissorsrock":
return "rock wins";
case "rockpaper": case "paperrock":
return "paper wins";
default: return "scissors wins";
}
};
if (choice1 === "rock") {
if (choice2 === "scissors");
} else {
return ("rock wins");
}
Look at this again. You say:
IF choice1 === rock, THEN
IF choice2 === scissors THEN DO NOTHING
ELSE (choice1 is not rock)
return 'rock wins'
This is a case where explicit braces would help. I'm guessing you meant to do this:
if (choice1 === "rock") {
if (choice2 === "scissors") {
}
} else {
return ("rock wins");
}
if the data is very controlled, you can just do:
if in (choice1+""+choice2) you find "ks", "rr" or "sp", you choice1
won, else lost (example below)
function getWinner(choice1, choice2){
var both_str, after_removing;
if(choice1 == choice2){
return "The result is a tie!";
}
both_str = (choice1 + "" + choice2);
after_removing = both_str.replace(RegExp("ks|rr|sp", "g"), "");
return (choice1 + ((both_str.length - after_removing.length) ? " won" : " lost"));
}
And you get the following results:
console.log(getWinner("scissors", "paper")); //scissors won
console.log(getWinner("rock", "scissors")); //rock won
console.log(getWinner("paper", "rock")); //paper won
console.log(getWinner("scissors", "rock")); //scissors lost
console.log(getWinner("rock", "paper")); //rock lost
console.log(getWinner("paper", "scissors")); //paper lost
console.log(getWinner("scissors", "scissors")); //The result is a tie!
console.log(getWinner("rock", "rock")); //The result is a tie!
console.log(getWinner("paper", "paper")); //The result is a tie!

I'm having trouble with the syntax for my compare function for javascript

I've included a screenshot image of a codeacademy error I made today. I'm trying to create a compare function that randomly picks a number between 0 and 1 (paper, scissors, or rock) that inputs two choices and returns the winner based on how choice1 compares to choice2.
The first part is a comment, but it explains how the original paper scissors rock function was built
Here is the 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";
}*/
var compare = function (choice1, choice2) {if (choice1 === choice2) return("The result is a tie!");
if (choice1 < 0.34)
if(choice2 ==="scissors");
return("rock wins");
} else if(choice2 ==="paper");{
return("paper wins");
};
};
It's telling me that there's an unexpected token else on line 15 (the else if line)
When I erase the else part it gives me another syntax error saying the same thing about a token if. I'm stuck on what part of my syntax is off and how to fix it.
I have a feeling it has something to do with === and the ; after the if() statements, either way, here is a nicer way of comparing them.
function compare(a,b)
{
if(a==b)return "draw";
switch(a)
{
case "rock":return (b=="scissors"?a:b)+" wins";
case "paper":return (b=="rock"?a:b)+" wins";
case "scissors":return (b=="paper"?a:b)+" wins";
}
}
console.log(compare("scissors","paper"));
function compare(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
}
if (choice1 < 0.34) {
if (choice2 === "scissors") {
return "rock wins";
} else if (choice2 === "paper") {
return "paper wins";
}
}
}
Check the comments on semicolon related errors below.
var compare = function (choice1, choice2) {
if (choice1 === choice2) return("The result is a tie!");
if (choice1 < 0.34) {
if(choice2 === "scissors") { // remove ; here
return("rock wins");
} else if (choice2 === "paper") { // remove ; here
return("paper wins");
} // remove ; here
} // add another else => what happens when choice1 >= 0.34 (not a rock)
};
With the required else blocks the complete function would look like:
var compare = function (choice1, choice2) {
if (choice1 === choice2) return("The result is a tie!");
if (choice1 < 0.34) { // rock
if(choice2 === "scissors") {
return("rock wins");
} else if (choice2 === "paper") {
return("paper wins");
}
} else if (choice <= 0.67) { // paper
if(choice2 === "rock") {
return("paper wins");
} else if (choice2 === "scissors") {
return("scissors wins");
}
} else { // scissors
if(choice2 === "paper") {
return("scissors wins");
} else if (choice2 === "rock") {
return("rock wins");
}
}
};
EDIT
This is just to help you overcome the confusion over semicolons if any. Usually a function definition is not required to have a ; after its body completes by putting the last closing curly brace }.
function compare (choice1, choice2) {
// ...
}
On the contrary, when we assign a variable a value, the statement is terminated by a semicolon.
var name = "John Doe";
Hence, when we combine the two, we're defining a function and then using it in an assignment statement which needs to be closed by using a semicolon. Hence, the syntax:
var compare = function (choice2, choice2) {
// ...
};

Categories

Resources