Reputation: 258
im trying to create the game "rock, paper or scissors" in JavaScript, but im stuck on the last function "game()", it should repeat 5 times the function playRound() and throw a result each of those 5 times. But doesnt work.
function computerPlay(){
let optionsList = ["rock", "paper", "scissors"];
let option = optionsList[Math.floor(Math.random() * optionsList.length)];
return option
}
function playRound(playerSelection, computerSelection){
var playerSelection = prompt("Choose: rock, paper or scissors").toLowerCase();
var computerSelection = computerPlay();
if (playerSelection = "rock"){
switch(computerSelection){
case "rock":
return "T";
case "paper":
return "M";
case "scissors":
return "Y";
}
}else if (playerSelection = "paper"){
switch(computerSelection){
case "rock":
return "Y";
case "paper":
return "T";
case "scissors":
return "M";
}
}else if (playerSelection = "scissors"){
switch(computerSelection){
case "rock":
return "M";
case "paper":
return "Y";
case "scissors":
return "T";
}
}
}
function game(){
let i = 0
for(i=1; i<=5; i++){
playRound()
if (playRound()= "M") {
console.log("Machine Wins");
}else if (playRound() = "Y"){
console.log("You Win")
}else if (playRound() = "T"){
console.log("You Win!")
}
}
}
Upvotes: 1
Views: 254
Reputation: 6735
You have a couple errors:
=
operator to compare values. You need to use ==
or ===
.playRound()
and then check its value to determine the outcome of the game. But instead, you are calling playRound()
initially in each iteration of your loop, and then again each time you try to determine the outcome.Try:
function computerPlay() {
let optionsList = ["rock", "paper", "scissors"];
let option = optionsList[Math.floor(Math.random() * optionsList.length)];
return option
}
function playRound(playerSelection, computerSelection) {
var playerSelection = prompt("Choose: rock, paper or scissors").toLowerCase();
var computerSelection = computerPlay();
if (playerSelection === "rock") {
switch (computerSelection) {
case "rock":
return "T";
case "paper":
return "M";
case "scissors":
return "Y";
}
} else if (playerSelection === "paper") {
switch (computerSelection) {
case "rock":
return "Y";
case "paper":
return "T";
case "scissors":
return "M";
}
} else if (playerSelection === "scissors") {
switch (computerSelection) {
case "rock":
return "M";
case "paper":
return "Y";
case "scissors":
return "T";
}
}
}
function game() {
let i = 0
for (i = 1; i <= 5; i++) {
let outcome = playRound()
if (outcome === "M") {
console.log("Machine Wins");
} else if (outcome === "Y") {
console.log("You Win")
} else if (outcome === "T") {
console.log("You Win!")
}
}
}
game();
Upvotes: 2
Reputation: 28128
Not sure it this fixes the problem but here's a syntax error: = instead of ==. Also, you shouldn't call playRound()
in every else
. Just store the result in a variable.
let result = playRound()
if (result== "M") {
console.log("Machine Wins");
}else if (result == "Y"){
console.log("You Win")
}else if (result == "T"){
console.log("You Win!")
}
Upvotes: 1