Java Script: Bina Permainan Rock, Paper, Scissors
Latihan membina game mudah.
- Declare a variable called userChoice.
- Make the variable equal to the answer we get by asking the user "Do you choose rock, paper or scissors?"
var userChoice = prompt("Do you choose rock, paper or scissors?");
- Under your previous code, declare a variable called computerChoice and make it equal to Math.random().
- Print out computerChoice so you can see how Math.random() works. This step isn't needed for the game - just useful for learning!
var userChoice = prompt("Do you choose rock, paper or scissors?");
computerChoice=Math.random();
console.log (computerChoice);We have computerChoice but it now equals a random number between 0 and 1. We need to somehow translate this random number into a random choice of rock, paper, or scissors. How do we do this?!
-If computerChoice is between 0 and 0.33, make computerChoice equal to "rock".
-If computerChoice is between 0.34 and 0.66, make computerChoice equal to "paper".
-If computerChoice is between 0.67 and 1, make computerChoice equal to "scissors".
var userChoice = prompt("Do you choose rock, paper or scissors?");
computerChoice=Math.random();
console.log (computerChoice);
if ((computerChoice >= 0) && (computerChoice <=0.33))
{
console.log (computerChoice);
console.log ("rock");
}
else if ((computerChoice >= 0.34) && (computerChoice <=0.66))
{
console.log (computerChoice);
console.log ("paper");
}
else
{
console.log (computerChoice);
console.log ("scissors");
}
-We carried over the code from the previous section, but it is a comment. Leave it there for now.
-Below the comment, declare a function called compare.
It takes two parameters, choice1 and choice2.
-Inside the function, write an if statement. If choice1 === choice2, then return "The result is a tie!"
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!";
}
};
compare(computerChoice,userChoice);
Computer: rock |
What if choice1 is "rock"? Given choice1 is "rock",
a. if choice2 === "scissors", then "rock" wins.
b. if choice2 === "paper", then "paper" wins.
How do we structure this? It's a bit different from what we have already seen. We will first have an if statement. And then the code inside that if statement will be... another if statement!
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
{
return "paper wins"
}
}
};
compare(computerChoice,userChoice);
SIAP!

1 ulasan:
Write ulasanPlease I need help to finish this course.
Replyvar 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 {
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 "rock wins";
}
}
};
compare(computerChoice,userChoice);
console.log(compare(computerChoice,userChoice));