Need help on 9/9


#1



Hey all, I decided to follow the last instruction for 9/9. I have added the scenario of somebody putting an invalid answer which worked (I checked). I then went ahead and tried to include rope as an option. I have no idea what rope does but I gave it a shot and I think my code works but I'm getting an error. I'm sure it's a tiny error like a typo of an extra space but I cannot find it. Please help :frowning:

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


SyntaxError: missing ( before condition


var userChoice = prompt("Do you choose rock, paper, scissors or rope?");
var computerChoice = Math.random();
if (computerChoice < 0.25) 
{
    computerChoice = "rock";
} 
else if(computerChoice <= 0.50) 
{
    computerChoice = "paper";
} 
else if{computerChoice <= 0.75) 
{
    computerChoice = "scissors";
} 
else 
{
    computerChoice = "rope";
}
} 
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(choice2 === "paper")
        {
            return"paper wins";
        }
        else
        {
            return"rope wins";
        }
    }
    else if(choice1 === "scissors")
    {
        if(choice2 === "rock")
        {
            return"rock wins";
        }
        else if(choice2 === "paper")
        {
            return"scissors wins";
        }
        else
        {
            return"scissors wins";
        }
    }
    else if(choice1 === "paper")
    {
        if(choice2 === "scissors")
        {
            return"scissors wins";
        }
        else if(choice2 === "rock")
        {
            return"paper wins";
        }
        else
        {
            return"rope wins"
        }
    }
    else(choice1 !== "rock", "paper", "scissors")
    {
        alert("Not a valid answer!");
    }
};
console.log(compare(userChoice , computerChoice));


#2

@xfrenchy,
An ELSE statement does not take a condition

else(choice1 !== "rock", "paper", "scissors")

1 To extend the userChoice-prompting with a validity-check, you would
embed the prompting into a function-body...
So you would start by
var getUserChoice = function() { your prompting here };

2 If you are extending the choice-possibillity, you have to extend the
so-called truth-table.

Your new Decisions-Table: fill-in ?? wins...

              | ch-2  |  ch-2  | ch-2     | ch-2   |
              | rock  |  paper | scissors | rope   |
  ------------|-------|--------|----------|--------|
              |       | paper  |  rock    | ????   |
 choice1  rock|   X   | wins   |  wins    |  wins  | (1st else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              | paper |        |scissors  |  ? ?   |
 choice1 paper| wins  |    X   |  wins    |  wins  | (2nd else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              |  rock |scissors|          |  ?  ?  | 
 ch-1 scissors|  wins | wins   |   X      |  wins  | (3rd else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              |  ? ?  | ? ? ?  |  ? ? ?   |        | 
 ch-1 rope    |  wins | wins   |   wins   |    X   | (4th else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|

3 What you want is a loop-construct...
create the getUserChoice function using return-statement
create the getComputerChoice function using return-statement
creat the compare function using return-statement ( allready done !!)

As you are using return-statements you call the compare-function
var theResult = compare(userChoice,computerChoice);
and thus having the =result= of the game in the theResult variable

==============================================================

var playTheGame =function() {
    var playAgain =true;
    var userChoice="";
    var computerChoice="";
    var theResult="";
    while (playAgain) {
    //execute the pre-declared getUserChoice-function
    userChoice = getUserChoice();
    console.log("User choice is " + userChoice);

```
        //execute the pre-declared getComputerChoice-function
        computerChoice = getComputerChoice();
        console.log("Computer choice is " + computerChoice);

        //execute the compare-function and capture the-result
        theResult = compare(userChoice,computerChoice);
        console.log("The result is " + theResult);

        //if (theResult === "It is a tie!") {
        //    console.log("It is a tie!");
        if (theResult === "The result is a tie!") {
            //console.log("The result is a tie!");
            playAgain = true;
            console.log("==== Play once more =====");
        } else {
            playAgain = false;
            console.log("==== End of Game =====");
        }

    //end of -while- loop is reached, and if VALUE of playAgain === true
    // the loop will start over
    } //closing the while-loop

}; //closing the function-body of playTheGame

##======================================================================

// call the function playTheGame
playTheGame();

#3

Thanks for the reply. That's weird though because I have ran the code with the else statement like that and it has worked fine. It broke down when I introduced the ropes but thanks for the feedback. I'll be trying to fix it later today and hopefully it will be working.


#4

Have a look at this line ^

//you have 
{.......) 
//instead of 
(.......)

#5

Omg thank you so much. I knew I had some type of error like this I just couldn't find it. Thank you


#6