9/9 SyntaxError: missing ) in parenthetical


#1
var userChoice=prompt("Do you choose rock, paper,or scissors?");
var computerChoice=Math.random();
var tie=true;

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";
    }
}
 console.log((compare userChoice,computerChoice))}
 var compare=function(choice1,choice 2){
 if (choice1===choice2){
return "The result is tie!";}
else {
return "dog wins";
}
}
else if (choice1==="rope"){
if(choice2==="sumo"){
return "rope wins";}
else {
return "sumo wins";
}

#2

The error you encounter is in this line:

console.log((compare userChoice,computerChoice))}

why does console.log() have two (( and two ))? what are trying to do here? call the function compare? Which i wouldn't do, since it is before you define the function. if you simple want to log the user choice and computer choice it should be console.log(userChoice + " " + computerChoice)


#3

I'm trying to extend the function to 'dog, rope, and sumo' wins if the game returns a tie. I don't understand what I'm doing wrong if I'm not supposed to call the function compare before I define the function? Aren't we working off of the previous exercise information, but different inappropriate choices for the players? Maybe you can point be in the right direction with the Syntax Error: expected expression, got keyword 'else'. Thanks!

var userChoice=prompt("Do you choose rock, paper,or scissors?");
var computerChoice=Math.random();
var tie=true;

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";
    }
}
 }  
    var compare=function(choice1, choice2){
    if (choice1===choice2){
    return "The result is tie!";}
    else {
    return "dog wins";
}
    }
else if (choice1==="rope"){
    if(choice2==="sumo"){
    return "rope wins";}
    else {
    return "sumo wins";
}

#4

okay, this code with rock paper and sciccors is fine:

var userChoice=prompt("Do you choose rock, paper,or scissors?");
var computerChoice=Math.random();
var tie=true;

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

now, lets talk about this bit:

var compare=function(choice1, choice2){
    if (choice1===choice2){
    return "The result is tie!";}
    else {
    return "dog wins";
}
    } /* this one */
else if (choice1==="rope"){
    if(choice2==="sumo"){
    return "rope wins";}
    else {
    return "sumo wins";
}

i inserted a comment (with text this one). the closing curly bracket on that line closes the function, it is to earlier, remove it.

i would give your compare function a different name, then i mean this one:

var compare=function(choice1, choice2){

for all i know you call it case_tie:

var case_tie=function(choice1, choice2){

then when a tie arise in your compare function (the one with rock, paper, scissors), call the case_tie function.

Never call a function before you define it. That would result in a bit of problem


#5

oh okay. I still have the same error message when incorporating your changes, so I'm not sure why its not allowing me to pass? does that mean that I opened or closed too soon or had some other error when trying to change the choice to rope, do I have to now identify the computerChoice and userChoice in this instance in order for the players to have new choices?


#6

Can i see the latest version of your code?


#7

var userChoice=prompt("Do you choose rock, paper,or scissors?");
var computerChoice=Math.random();
var tie=true;
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";
}
}
}
var case_tie=function(choice1, choice2){
if (choice1===choice2){
return "The result is tie!";}
else {
return "dog wins";
}
else if (choice1==="rope"){
if(choice2==="sumo"){
return "rope wins";}
else }
return "sumo wins";
}


#8

You can't do this:

if (){}
else (){}
else if (){}

you can't have a else if statement after a else statement, and you are missing some closing curly brackets to close your case_tie function


#9

thanks so much for the explanation they were a major help and help me to understand the process better.


#10

If you include else, it should be last, you can do nesting of course:

if (){
  if(){}
  else{}
}
else if (){
  if(){}
  else{}
}
else {}

as you can see, else is always last