I have a function{computerPlay()} to generate a new random number every time it is called. It returns an item from the array declared at the top. It works how I'd like it to when I test it in the console. However, in order for my game to work properly, I've put it in a for loop and it keeps returning the same value unless I refresh the page. I can't find an answer anywhere and this is my first question. I'm sure the code is sloppy so please ignore that. Here is the code.
let playOptionsArray = ["Rock", "Paper", "Scissors"];
let totalTies = 0;
let playerPoints = 0;
let computerPoints = 0;
let round = 1;
//formats a player's entry so it will match playOptionsArray
function firstLetterUppercase(string){
let firstLetter = string.charAt(0);
let firstLetterCapitalized = firstLetter.toUpperCase();
let restOfResponse = string.slice(1);
let formattedResponse = firstLetterCapitalized + restOfResponse;
return formattedResponse;
}
//compares player entry to ensure it matches an item on the playOptions array
function checkPlayerResponse(response){
if (response === "Rock" || response === "Paper" || response === "Scissors") {
return response;
} else {
return alert("You must choose either 'Rock', 'Paper', or 'Scissor' as your play. Please try again.");
}
}
//generates a random option for the computer's play
function computerPlay() {
let computerSelection = playOptionsArray[Math.floor(Math.random()*(playOptionsArray.length))];
return computerSelection;
}
let computerSelection = computerPlay();
function getPlayerSelection() {
let playerEntry = window.prompt("You are playing the computer in a game of Rock, Paper, Scissors. First player to five points wins. Choose either Rock, Paper, or Scissors as your play", "Rock");
if (playerEntry != null) {
let playerEntryLowercase = playerEntry.toLowerCase();
playerSelection = firstLetterUppercase(playerEntryLowercase);
} else {
getPlayerSelection();
}
checkPlayerResponse(playerSelection);
return playerSelection;
}
let playerSelection = getPlayerSelection;
//emulates a single round of rock paper scissor
function singleRound(){
if (playerSelection === "Rock" && computerSelection === "Scissors"){
alert('You have won this round. The computer chose ' + computerSelection + '.');
playerPoints++;
round++;
} else if (playerSelection === "Scissors" && computerSelection === "Paper"){
alert('You have won this round. The computer chose ' + computerSelection + '.');
playerPoints++;
round++
} else if (playerSelection === "Paper" && computerSelection === "Rock"){
alert('You have won this round. The computer chose ' + computerSelection + '.');
playerPoints++;
round++
} else if (playerSelection === computerSelection){
alert('You have tied this round. The computer chose ' + computerSelection + '.');
totalTies++;
} else {
alert('Something went wrong. Refresh the page.')
}
}
function updateScore() {
document.getElementById("player-points").innerHTML = playerPoints;
document.getElementById("round").innerHTML = round;
document.getElementById("computer-points").innerHTML = computerPoints;
document.getElementById("total-ties").innerHTML = totalTies;
}
//keeps track of player points and ends when a player gets 5 points
function game() {
for (round = 0; computerPoints < 5 && playerPoints < 5; round) {
getPlayerSelection();
computerPlay();
singleRound();
updateScore();
}
}
game();
gameyou're not doing anything with the returned value fromcomputerPlay. Is that intentional?computerPlaybut your round scoring checks a global variablecomputerSelectionyou only set once (the localcomputerSelectioninside the function shadows the global). Congratulations! You just learned that side-effects are evil. Burned hands teach best and whatnot.let playerSelection = getPlayerSelection;should belet playerSelection = getPlayerSelection();if you want it to do anything.