I have one or two questions about how to "randomize" a result on this game when you type the exact same choice.
e.g: I'm calling the function 5 times and if I type different choices (first rock, then paper and then scissors, etc..) it's okay, it throws different random results, but if I type one of them three or five times, like for e.g, paper, it will throw the same result everytime no matter what, and I would like to have different results on it, is this possible without using loops? I'm following TOP courses and I would like not to use loops until I get to that part.
Also, I would like to know if there is some way to print "Machine wins" or "Human wins" after all the attemps, depending on who wins, like a final message for winning or losing all rounds.
Here is the code:
function computerPlay() {
const choice = ["rock", "paper", "scissors"];
const choiceRandom = choice[Math.floor(Math.random() * choice.length)];
return choiceRandom;
}
function game(computerSelection, playerSelection) {
playerSelection = prompt("Rock, paper or scissors?").toLowerCase();
if (computerSelection=="rock" && playerSelection=="scissors") {
console.log("You lose, rock beats scissors");
} else if (computerSelection=="paper" && playerSelection=="scissors") {
console.log("You win, scissors beats paper!");
} else if (computerSelection=="rock" && playerSelection=="paper") {
console.log("You lose, rock beats paper!");
} else if (computerSelection=="paper" && playerSelection=="rock") {
console.log("You lose, paper beats rock!");
} else if (computerSelection=="scissors" && playerSelection=="rock") {
console.log("You win, rock beats scissors!");
} else if (computerSelection=="scissors" && playerSelection=="paper") {
console.log("You lose, scissors beats paper!");
} else if (computerSelection===playerSelection) {
console.log("Draw!");
}
}
const computerSelection = computerPlay();
let playerSelection
game(computerSelection, playerSelection);
game(computerSelection, playerSelection);
game(computerSelection, playerSelection);
game(computerSelection, playerSelection);
game(computerSelection, playerSelection);
I'm blank at this, have been trying to figure it out for hours.
Thank you.