Rock paper scissors next step


#1

so..in this chapter we should use loop..the statement we haven't studied yet?


Rock, paper & scissors: Next step
#2

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 validateUserChoice = function (userChoice) {
  do {
    userChoice = prompt("Choose either rock, paper or scissors");
  } while (userChoice !== "rock" || userChoice !== "paper" || userChoice !== "scissors");
}

var compare = function(choice1, choice2){ ///opens the function
if(choice1 === choice2){ ///opens the if
    return("The result is a tie!");
} ///closes the if
else if (choice1 === "rock"){ ///opens the else if
if(choice2 === "scissors"){ ///opens the if
return("rock wins");
}  ///closes the if
else{ ///opens the else
return("paper wins")
 } ///closes the else
} ///closes the else if 
else if (choice1 === "paper") { ///opens the else if
if (choice2 === "rock") { ///opens the if
return("paper wins");
} ///closes the if
else { ///opens the else
return("scissors wins");
  } ///closes the else
    
    } ///closes the else if
else if (choice1 === "scissors") { ///opens the else if
if (choice2 === "rock") { ///opens the if
return("rock wins");
} ///closes the if
else { ///opens the else
 return("scissors wins");
} ///closes the else
} ///closes the else if

}; ///closes the function

compare (userChoice, computerChoice);

#3

@stetim94 I input your loop but it doesn't work..Please help find out what's wrong with it?


#4

the problem seems to be in this line:

while (userChoice !== "rock" || userChoice !== "paper" || userChoice !== "scissors");

you want to check that the users input isn't rock and isn't paper, and isn't scissors, so you want to use && rather then ||.


#5

@keundo, @stetim94,

I would create a getUserChoice() function,
which return's a userChoice-VALUE

var getUserChoice = function () {
 var userChoice;
 do {
      userChoice = prompt("Choose either rock, paper or scissors");
     } while (userChoice !== "rock" &&
              userChoice !== "paper" &&
              userChoice !== "scissors");
 //console.log("UserChoice is : "+ userChoice);
 return userChoice;
};

// var userChoice = getUserChoice();

and a getComputerChoice() function
which return's a computerChoice-VALUE

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: " + computerChoice);
  return computerChoice;
}

You could then use.....

[done] create the getUserChoice function using return-statement
[done] create the getComputerChoice function using return-statement
[done] create the compare function using return-statement

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
       var userChoice = getUserChoice();
       console.log("User choice is " + userChoice);

        //execute the pre-declared getComputerChoice-function
        var 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!") {
       playAgain = true;
    } else {
      playAgain = false;
    }
    //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();

#6

I changed for &&, and it doesn't work either..Is says:"Ops, tente outra vez. There was a problem with your syntax.SyntaxError: Unexpected token !==".

What's wrong with "!=="?


#7

Last part doesn't work..It says:"UserChoice is : rock
Computer choice is: rock
UserChoice is : rock
User choice is rock
ReferenceError: getComputerChoice is not defined"


#8

Then i guess something went wrong, could you post an updated version of your code?

for next time, use one of the two following options to make your code/indent is visible:

select your code and press ctrl + shift + c (or cmd + shift + c if you use a mac)

if this instructions are unclear, you can also insert 3 backticks before and after your code, like so:

```
<p>visible</p>
```

the backtick is located above the tab key on your keyboard


#9

I use laptop. Whenever I press ctrl + shift + c, my google chrome browser opens Developer tool I don't know what to do with..

Below is the updated version of my code. I've just inserted your parts. I'm not familiar with loops yet that is the topik of next chapter..


#10

var userChoice = prompt("Do you choose rock, paper or scissors?");

var getUserChoice = function () {
 var userChoice;
 do {
      userChoice = prompt("Oops! Please choose either rock, paper or scissors");
     } while (userChoice !== "rock" &&
              userChoice !== "paper" &&
              userChoice !== "scissors");
 console.log("UserChoice is : "+ userChoice);
 return userChoice;
};
userChoice = getUserChoice();

var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer choice is: " + computerChoice);

var compare = function(choice1, choice2){ ///opens the function
if(choice1 === choice2){ ///opens the if
    return("The result is a tie!");
} ///closes the if
else if (choice1 === "rock"){ ///opens the else if
if(choice2 === "scissors"){ ///opens the if
return("rock wins");
}  ///closes the if
else{ ///opens the else
return("paper wins")
 } ///closes the else
} ///closes the else if 
else if (choice1 === "paper") { ///opens the else if
if (choice2 === "rock") { ///opens the if
return("paper wins");
} ///closes the if
else { ///opens the else
return("scissors wins");
  } ///closes the else
    
    } ///closes the else if
else if (choice1 === "scissors") { ///opens the else if
if (choice2 === "rock") { ///opens the if
return("rock wins");
} ///closes the if
else { ///opens the else
 return("scissors wins");
} ///closes the else
} ///closes the else if

}; ///closes the function

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

        //execute the pre-declared getComputerChoice-function
        var 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!") {
       playAgain = true;
    } else {
      playAgain = false;
    }
    //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();

#11

This are @leonhard.wettengmx.n parts, i have to go to school. No time to through this parts, leonhard, can you look into it?


#12

@keundo,

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

//=================================

var compare = function(choice1, choice2){ ///opens the function
  if(choice1 === choice2){ ///opens the if
      return("The result is a tie!");
  } ///closes the if

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

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

  else if (choice1 === "scissors") { ///opens the else if
      if (choice2 === "rock") { ///opens the if
          return("rock wins");
      } ///closes the if
      else { ///opens the else
          return("scissors wins");
      } ///closes the else
  } ///closes the else if

}; ///closes the function

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

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

#13

It's still doesn't work..it says:"Ops, tente outra vez. There was a problem with your syntax. SyntaxError: Unexpected token ==="