Javascript Build "Rock, Paper, Scissors" Lesson 9


#1


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


Oops, try again. There was a problem with your syntax.


var userChoice = prompt("Do you choose rope, rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.25) {
	computerChoice = "rock";
} else if(computerChoice <= 0.50) {
	computerChoice = "paper";
} else if(computerChoice <= 0.75) {
	computerChoice = "rope";
}else if(computerChoice <= 1) {
	computerChoice = "scissors";
} else{
    return "choose from rope, rock, paper or scissors";
}console.log("Computer: " + computerChoice);

var compare = function(choice1,choice2)  {
    if (choice1 === choice2)  {
        console.log("The result is a tie!);
        var userChoice = prompt("Do you choose rope, rock, paper or scissors?");
    
    } else if  ( choice1 === "paper")  { 

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

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

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

compare(userChoice,computerChoice);

I want to add " if both players make the same choice, the game returns a tie. What if the game didn't end there but instead asked both players for new choices?" , but it doesn't work.


Syntax error please help
#2

In your console.log statement, "your result is a tie, you need a closing quote.


#3

Thanks. but I still have an error "SyntaxError: Return statements are only valid inside functions."


#4

Yeah. You need to go and heck to make sure every beginning bracket has a closing one. On some else if's, I couldn't find closing brackets for them.


#5

In your IF ELSE-IF at the top of your code
your using an ELSE
you are using the return statement....and that is NOT inside a function!!!


#6

Thanks. I changed my code a little bit and It works.


#7

@kkrayp,

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 getUserChoice = function () {
 var userChoice;
 do {
      userChoice = prompt("Choose either rock, paper or scissors","paper");
     } while (userChoice !== "rock" &&
          userChoice !== "paper" &&
          userChoice !== "scissors");
     // console.log("UserChoice is 1: "+ userChoice);
     return userChoice;
};
var getComputerChoice = function() {
var computerChoice = Math.random();
  if (computerChoice < 0.34) {
    computerChoice = "rock";
  } else if(computerChoice <= 0.67) {
	computerChoice = "paper";
  } else {
	computerChoice = "scissors";
  }
  //console.log("computerChoice 1: " + computerChoice);
  return computerChoice;
};

//var userChoice = getUserChoice();
//var computerChoice = getComputerChoice();

#8

@kkrayp

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();
`