I am currently trying to learn Javascript, and I am trying to make a simple Rock, Paper, Scissors application. At the moment my code looks like this
function computerPlay() {
let options = ['Rock', 'Paper', 'Scissors'];
return options[Math.floor(Math.random() * options.length)]
}
computerWin = 0;
userWin = 0;
const buttons = document.querySelectorAll('button');
buttons.forEach((button) => {
button.addEventListener('click', () => {
computerSelection = computerPlay();
playerSelection = button.id;
var userScore = document.getElementById('userScore');
var computerScore = document.getElementById('computerScore');
const result = playRound(playerSelection, computerSelection);
if (result == 1) {
userWin++;
userScore.nodeValue = Number(userWin);
} else if (result == 2) {
computerWin++;
computerScore.nodeValue = Number(computerWin);
}
});
});
function playRound(playerSelection, computerSelection) {
playerSelection = playerSelection.toLowerCase();
computerSelection = computerSelection.toLowerCase();
let playerWon = 1
let computerWon = 0
let tie = 2
if (playerSelection == computerSelection) {
return tie;
}
if (playerSelection === "rock") {
if (computerSelection === "scissors") {
return playerWon;
}
if (computerSelection === "paper") {
return computerWon;
}
}
if (playerSelection === "scissors") {
if (computerSelection === "paper") {
return playerWon;
}
if (computerSelection === "rock") {
return computerWon;
}
}
if (playerSelection === "paper") {
if (computerSelection === "rock") {
return playerWon;
} else {
return computerWon;
}
}
}
<button id="rock">ROCK</button>
<button id="paper">PAPER</button>
<button id="scissors">SCISSORS</button>
<br>
<p>Vlad : </p>
<input type="text" id="userScore" value="0" />
<p>Computer : </p>
<input type="text" id="computerScore" value="0" />
When I press any of the buttons, the round is played, a result is returned, but the value inside the labels never changes.
You should use .value and not .nodeValue
Rock Paper Scissor Video Tutorial
function computerPlay() {
let options = ['Rock', 'Paper', 'Scissors'];
return options[Math.floor(Math.random() * options.length)]
}
computerWin = 0;
userWin = 0;
const buttons = document.querySelectorAll('button');
buttons.forEach((button) => {
button.addEventListener('click', () => {
computerSelection = computerPlay();
playerSelection = button.id;
var userScore = document.getElementById('userScore');
var computerScore = document.getElementById('computerScore');
const result = playRound(playerSelection, computerSelection);
if (result == 1) {
userWin++;
userScore.value = Number(userWin);
} else if (result == 2) {
computerWin++;
computerScore.value = Number(computerWin);
}
});
});
function playRound(playerSelection, computerSelection) {
playerSelection = playerSelection.toLowerCase();
computerSelection = computerSelection.toLowerCase();
let playerWon = 1
let computerWon = 0
let tie = 2
if (playerSelection == computerSelection) {
return tie;
}
if (playerSelection === "rock") {
if (computerSelection === "scissors") {
return playerWon;
}
if (computerSelection === "paper") {
return computerWon;
}
}
if (playerSelection === "scissors") {
if (computerSelection === "paper") {
return playerWon;
}
if (computerSelection === "rock") {
return computerWon;
}
}
if (playerSelection === "paper") {
if (computerSelection === "rock") {
return playerWon;
} else {
return computerWon;
}
}
}
<button id="rock">ROCK</button>
<button id="paper">PAPER</button>
<button id="scissors">SCISSORS</button>
<br>
<p>Vlad : </p>
<input type="text" id="userScore" value="0" />
<p>Computer : </p>
<input type="text" id="computerScore" value="0" />
you need to set the value of the input. Also, your logic for result was a little messed up. Take a look at the following and let me know if this works for you.
function computerPlay() {
let options = ['Rock', 'Paper', 'Scissors'];
return options[Math.floor(Math.random() * options.length)]
}
computerWin = 0;
userWin = 0;
const buttons = document.querySelectorAll('button');
buttons.forEach((button) => {
button.addEventListener('click', () => {
computerSelection = computerPlay();
playerSelection = button.id;
var userScore = document.getElementById('userScore');
var computerScore = document.getElementById('computerScore');
const result = playRound(playerSelection, computerSelection);
if (result === 1) {
userWin++;
userScore.value = userWin;
} else if (result === 0) {
computerWin++;
computerScore.value= computerWin;
}else if(result === 2){
userWin= userWin + .5;
userScore.value = userWin;
computerWin=computerWin + .5;
computerScore.value= computerWin;
}
});
});
function playRound(playerSelection, computerSelection) {
playerSelection = playerSelection.toLowerCase();
computerSelection = computerSelection.toLowerCase();
let playerWon = 1
let computerWon = 0
let tie = 2
if (playerSelection == computerSelection) {
return tie;
}else if (playerSelection === "rock") {
if (computerSelection === "scissors") {
return playerWon;
}
if (computerSelection === "paper") {
return computerWon;
}
}else if (playerSelection === "scissors") {
if (computerSelection === "paper") {
return playerWon;
}
if (computerSelection === "rock") {
return computerWon;
}
}else if (playerSelection === "paper") {
if (computerSelection === "rock") {
return playerWon;
} else {
return computerWon;
}
}
}
<button id="rock">ROCK</button>
<button id="paper">PAPER</button>
<button id="scissors">SCISSORS</button>
<br>
<p>Vlad : </p>
<input type="text" id="userScore" value="0" />
<p>Computer : </p>
<input type="text" id="computerScore" value="0" />
Related
I am working on a game of rock, paper, scissors in the course "The Odin Project." Unfortunately, I've been at a standstill for several days because I can't figure out how to set the outcome of the duel. I am uploading my code below. I am a beginner so please understand :) I would appreciate your help.
const choice = ['rock', 'paper', 'scissors']
function getComputerChoice() {
const computerPlay = choice[Math.floor(Math.random() * choice.length)]
return computerPlay
}
function playRound(playerSelection, computerSelection) {
let YouLose = `You lose ${computerSelection} beats ${playerSelection.toLowerCase()}!`
let YouWin = `You win ${playerSelection.toLowerCase()} beats ${computerSelection}!`
if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'paper') {
return YouLose
score ++
} else if (playerSelection.toLowerCase() === 'paper' && computerSelection === 'rock') {
return YouWin
}
else if (playerSelection.toLowerCase() === 'paper' && computerSelection === 'scissors') {
return YouLose
}
else if (playerSelection.toLowerCase() === 'scissors' && computerSelection === 'paper') {
return YouWin
} else if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'scissors') {
return YouLose
}
else if (playerSelection.toLowerCase() === 'scissors' && computerSelection === 'rock') {
return YouLose
}else if(playerSelection.toLowerCase() === computerSelection) {
return "Tie!"
} else {
return "Error"
}
}
// function checkWinner() {
// if (playRound(playerSelection, computerSelection) === YouWin) {
// playerScore++;
// }else if (playRound(playerSelection, computerSelection) === YouLose) {
// computerScore++;
// }
// }
function game() {
console.log("Let's play the game!");
let playerScore = 0
let computerScore = 0
for (let i = 0; i < 5; i++) {
const playerSelection = prompt('What do you choose, rock, scissors or paper?')
const computerSelection = getComputerChoice()
playRound(playerSelection, computerSelection)
if (playRound(playerSelection.toLowerCase(), computerSelection) === YouWin) {
playerScore++;
}else if (playRound(playerSelection.toLowerCase(), computerSelection) === YouLose) {
computerScore++;
}
console.log(playRound(playerSelection, computerSelection));
console.log('-------------------');
console.log(playerScore);
console.log(computerScore);
}
}
console.log(game());
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="script.js"></script>
<title>Document</title>
</head>
<body>
<script>
// function computerPlay() {
// let game = ['rock', 'paper' , 'scissors'];
// let randomPlay = Math.floor(Math.random()*game.length);
// return game[randomPlay]
// }
// let player = 0
// let computer = 0
// let round = 0
// function playRound(playerSelection, computerSelection) {
// if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'paper') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// } else if (playerSelection.toLowerCase() === 'paper' && computerSelection === 'rock') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// }
// else if (playerSelection.toLowerCase() === 'paper' && computerSelection === 'scissors') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// }
// else if (playerSelection.toLowerCase() === 'scissors' && computerSelection === 'paper') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// } else if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'scissors') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// }
// else if (playerSelection.toLowerCase() === 'scissors' && computerSelection === 'rock') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// } else if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'rock') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// } else if (playerSelection.toLowerCase() === 'paper' && computerSelection === 'paper') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// } else if (playerSelection.toLowerCase() === 'scissors' && computerSelection === 'scissors') {
// computer++;
// return "You Win! Paper beats Rock";
// console.log(computer);
// } else {
// return "Erorr"
// }
// console.log(computer);
// }
// function game () {
// for (let i = 0; i < 5; i++) {
// playerSelection = prompt('What do you choose, rock, scissors or paper?');
// computerSelection = computerPlay();
// console.log(playRound(playerSelection, computerSelection));
// }
// }
// // console.log(computerSelection);
// // console.log(playerSelection);
// console.log(game());
</script>
</body>
</html>
https://github.com/RadekLewandowski/rock-paper-scissors/blob/6f60d9721a3f7e7835e5e9254cb26fcb766bedc2/script.js
i just fixed your ideas:
const choice = ['rock', 'paper', 'scissors']
function getComputerChoice() {
const computerPlay = choice[Math.floor(Math.random() * choice.length)]
return computerPlay
}
function playRound(playerSelection, computerSelection) {
if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'paper') {
return 'YouLose'
score ++
} else if (playerSelection.toLowerCase() === 'paper' && computerSelection === 'rock') {
return 'YouWin'
}
else if (playerSelection.toLowerCase() === 'paper' && computerSelection === 'scissors') {
return 'YouLose'
}
else if (playerSelection.toLowerCase() === 'scissors' && computerSelection === 'paper') {
return 'YouWin'
} else if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'scissors') {
return 'YouLose'
}
else if (playerSelection.toLowerCase() === 'scissors' && computerSelection === 'rock') {
return 'YouLose'
}else if(playerSelection.toLowerCase() === computerSelection) {
return "Tie!"
} else {
return "Error"
}
}
function game() {
console.log("Let's play the game!");
let playerScore = 0
let computerScore = 0
for (let i = 0; i < 5; i++) {
const playerSelection = prompt('What do you choose, rock, scissors or paper?')
const computerSelection = getComputerChoice()
const outcome = playRound(playerSelection, computerSelection)
if (outcome === 'YouWin')
{
alert(`You win ${playerSelection.toLowerCase()} beats ${computerSelection}!`)
playerScore++;
}
else if (outcome === 'YouLose')
{
computerScore++;
alert(`You lose ${computerSelection} beats ${playerSelection.toLowerCase()}!`)
}
console.log(playRound(playerSelection, computerSelection));
console.log('-------------------');
console.log(playerScore);
console.log(computerScore);
}
}
console.log(game());
Applied lots of fixes, simplified the code which determines whether you win or lose and ended up with this:
const choice = ['rock', 'paper', 'scissors']
function getComputerChoice() {
const computerPlay = choice[Math.floor(Math.random() * choice.length)]
return computerPlay
}
let YouLose;
let YouWin;
function playRound(playerSelection, computerSelection) {
YouLose = `You lose ${computerSelection} beats ${playerSelection.toLowerCase()}!`;
YouWin = `You win ${playerSelection.toLowerCase()} beats ${computerSelection}!`;
let diff = ((choice.indexOf(playerSelection.toLowerCase()) - choice.indexOf(computerSelection.toLowerCase())) + 3) % 3;
switch (diff) {
case 1: return YouWin;
case 2: return YouLose;
default: return "Tie!";
}
}
// function checkWinner() {
// if (playRound(playerSelection, computerSelection) === YouWin) {
// playerScore++;
// }else if (playRound(playerSelection, computerSelection) === YouLose) {
// computerScore++;
// }
// }
function game() {
console.log("Let's play the game!");
let playerScore = 0
let computerScore = 0
function round(index) {
let playerSelection = prompt('What do you choose, rock, scissors or paper?');
while (choice.indexOf(playerSelection ? playerSelection.toLowerCase() : "") < 0) {
playerSelection = prompt('Invalid choice. What do you choose, rock, scissors or paper?');
}
const computerSelection = getComputerChoice();
let result = playRound(playerSelection.toLowerCase(), computerSelection.toLowerCase());
if (result === YouWin) {
playerScore++;
}else if (result === YouLose) {
computerScore++;
}
alert(result);
console.log('-------------------');
console.log('Player: ' + playerScore);
console.log('Computer: ' + computerScore);
setTimeout(function() {
round(index + 1);
}, 1000);
}
round(0);
}
console.log(game());
Whenever I run the code , computer-Point has score 1 before the game
started . Second issue that I couldn't figure out is that how to set a
play round for 5 set. these are the JavaScript code
describe the values
set computer choices
set check-winner Function
set Player choices
set game function
````
var playerPoint = 0;
var computerPoint = 0;
const userScore_p = document.querySelector(".human > p");
const computerScore_p = document.querySelector(".machine > p");
const result_p = document.querySelector('.result > p');
const rock = document.getElementById('rock')
const paper = document.getElementById('paper');
const scissors = document.getElementById('scissors');
const playerSelection = [rock, paper, scissors]
const weapons = ["rock", "paper", "scissors"];
// set the computer random choice
function getComputerChoice(){
const choice = weapons[Math.floor(Math.random() * weapons.length)];
return choice;
}
function checkWinner(playerSelection) {
const computerSelection = getComputerChoice();
if (playerSelection == computerSelection) {
result_p.textContent = "Tie";
}
else if ((playerSelection == "rock" && computerSelection == "scissors") ||
(playerSelection == "scissors" && computerSelection == "paper") ||
(playerSelection == "paper" && computerSelection == "rock")) {
result_p.textContent = "player Winnnn";
playerPoint++;
userScore_p.innerHTML = playerPoint;
} else {
result_p.textContent = "Player lost!!!!";
computerPoint++;
computerScore_p.innerHTML = computerPoint;
}
console.log(computerSelection)
console.log(playerSelection);
}
function getPlayerChoice() {
rock.addEventListener("click", function () {
checkWinner("rock");
});
paper.addEventListener("click", function() {
checkWinner("paper");
});
scissors.addEventListener("click", function() {
checkWinner("scissors")
});
}
function game() {
const computerSelection = getComputerChoice();
for (let i = 0; i < 5; i++) {
if (playerPoint > computerPoint) {
result_p.innerHTML = "you won the game"
} else if (playerPoint < computerPoint) {
result_p.innerHTML = "you lose the game"
} else {
result_p.innerHTML = "Drawwww"
}
}
}
game()
checkWinner();
getPlayerChoice();
````
The problem here is, player is not going to choose a weapon until they click on 1 of the 3 buttons, so when you call the function getPlayerChoice() it actually doesn't mean the player clicked on one of those buttons.
You should process the logics inside the checkWinner() function, which is called whenever the user clicks on a button.
let playerPoint = 0;
let computerPoint = 0;
const userScore_p = document.querySelector(".human > p");
const computerScore_p = document.querySelector(".machine > p");
const result_p = document.querySelector('.result > p');
const rock = document.getElementById('rock')
const paper = document.getElementById('paper');
const scissors = document.getElementById('scissors');
const playerSelection = [rock, paper, scissors]
rock.addEventListener("click", function() {
checkWinner("rock");
});
paper.addEventListener("click", function() {
checkWinner("paper");
});
scissors.addEventListener("click", function() {
checkWinner("scissors")
});
const weapons = ["rock", "paper", "scissors"];
// set the computer random choice
function getComputerChoice() {
const choice = weapons[Math.floor(Math.random() * weapons.length)];
return choice;
}
function checkWinner(playerSelection) {
const computerSelection = getComputerChoice();
if (playerSelection == computerSelection) {
result_p.textContent = "Tie";
} else if ((playerSelection == "rock" && computerSelection == "scissors") ||
(playerSelection == "scissors" && computerSelection == "paper") ||
(playerSelection == "paper" && computerSelection == "rock")) {
result_p.textContent = "player Winnnn";
playerPoint++;
userScore_p.innerHTML = playerPoint;
} else {
result_p.textContent = "Player lost!!!!";
computerPoint++;
computerScore_p.innerHTML = computerPoint;
}
console.log(computerSelection)
console.log(playerSelection);
}
<button id="rock">rock</button>
<button id="paper">paper</button>
<button id="scissors">scissors</button>
<div>
HUMAN
<div class="human">
<p>0</p>
</div>
</div>
<div>
MACHINE
<div class="machine">
<p>0</p>
</div>
</div>
<div>
RESULT
<div class="result">
<p></p>
</div>
</div>
I am trying to make a small rock, paper and scissors program between a user and the computer. Everything seems to work fine except the gameRound() conditional statement. It doesn't matter what the user input is. It just runs the (else) statement.
// a function that determines what the computer's choice is
const getComputerChoice = () => {
let compChoice = Math.random();
if(compChoice > 0.5) {
return "Rock";
} else if(compChoice < 0.5) {
return "scissors";
} else if(compChoice == 0) {
return "paper";
}
}
// prompts the user to enter rock, paper or scissors
let playerSelection = prompt();
let computerSelection = getComputerChoice();
// the logic that decides who wins and loses
const gameRound = (playerSelection, computerSelection) => {
if(playerSelection === "rock" && computerSelection === "rock" || playerSelection === "paper" && computerSelection === "paper" || playerSelection === "scissors" && computerSelection === "scissors") {
return "It's a tie!!!";
} else if(playerSelection === "rock" && computerSelection === "paper") {
return "Computer wins!!!";
} else if(playerSelection === "rock" && computerSelection === "scissors") {
return "You win!!!";
} else if(playerSelection === "paper" && computerSelection === "scissors") {
return "Computer wins!!!";
} else if(playerSelection === "paper" && computerSelection === "rock") {
return "You win!!!";
} else if(playerSelection === "scissors" && computerSelection === "rock") {
return "Computer wins!!!";
} else if(playerSelection === "scissors" && computerSelection === "paper") {
return "You win!!!";
} else {
return "Winner undecided";
}
}
// the function that begins the game
const game = () => {
for(let i = 0; i < 5; i++) {
return gameRound();
}
}
console.log(game());
gameRound conditional statement doesn’t function as expected due to the newly declared parameters playerSelection, computerSelection. So it has to be a function statement rather than the inline arrow function.
Then, Javascript code would be as follows:
// a function that determines what the computer's choice is
const getComputerChoice = () => {
let compChoice = Math.random();
if(compChoice > 0.5) {
return "Rock";
} else if(compChoice < 0.5) {
return "scissors";
} else if(compChoice == 0) {
return "paper";
}
}
// the logic that decides who wins and loses
function gameRound(playerSelection, computerSelection) {
if(playerSelection === "rock" && computerSelection === "rock" || playerSelection === "paper" && computerSelection === "paper" || playerSelection === "scissors" && computerSelection === "scissors") {
return "It's a tie!!!";
} else if(playerSelection === "rock" && computerSelection === "paper") {
return "Computer wins!!!";
} else if(playerSelection === "rock" && computerSelection === "scissors") {
return "You win!!!";
} else if(playerSelection === "paper" && computerSelection === "scissors") {
return "Computer wins!!!";
} else if(playerSelection === "paper" && computerSelection === "rock") {
return "You win!!!";
} else if(playerSelection === "scissors" && computerSelection === "rock") {
return "Computer wins!!!";
} else if(playerSelection === "scissors" && computerSelection === "paper") {
return "You win!!!";
} else {
return "Winner undecided";
}
}
// prompts the user to enter rock, paper or scissors
let playerSelection = prompt();
let computerSelection = getComputerChoice();
for (let i = 0; i < 5; i++) {
console.log(gameRound(playerSelection, computerSelection));
}
Or if you mean to play 5 rounds of the rock-paper-scissors, the allocation of playerSelection and computerSelection variables should be inside the for loop as follows:
// a function that determines what the computer's choice is
const getComputerChoice = () => {
let compChoice = Math.random();
if(compChoice > 0.5) {
return "Rock";
} else if(compChoice < 0.5) {
return "scissors";
} else if(compChoice == 0) {
return "paper";
}
}
// the logic that decides who wins and loses
function gameRound(playerSelection, computerSelection) {
if(playerSelection === "rock" && computerSelection === "rock" || playerSelection === "paper" && computerSelection === "paper" || playerSelection === "scissors" && computerSelection === "scissors") {
return "It's a tie!!!";
} else if(playerSelection === "rock" && computerSelection === "paper") {
return "Computer wins!!!";
} else if(playerSelection === "rock" && computerSelection === "scissors") {
return "You win!!!";
} else if(playerSelection === "paper" && computerSelection === "scissors") {
return "Computer wins!!!";
} else if(playerSelection === "paper" && computerSelection === "rock") {
return "You win!!!";
} else if(playerSelection === "scissors" && computerSelection === "rock") {
return "Computer wins!!!";
} else if(playerSelection === "scissors" && computerSelection === "paper") {
return "You win!!!";
} else {
return "Winner undecided";
}
}
for (let i = 0; i < 5; i++) {
// prompts the user to enter rock, paper or scissors
let playerSelection = prompt();
let computerSelection = getComputerChoice();
console.log(gameRound(playerSelection, computerSelection));
}
Currently working on TOP rock paper scissors game and I'm having some trouble. The function playRound is showing up as undefined in the console. I'm not sure why as all my other function are working. Maybe i did something wrong in the other functions but I'm not sure. I feel like I'm so close yet so far from this thing working. Maybe it's just common beginner mistakes not sure. Any help would be appreciated.
function computerPlay() {
var pickRandom = ["Rock", "Paper", "Scissors"];
var randomMove = pickRandom[Math.floor(Math.random() * 3)];
if (randomMove == "Rock") {
randomMove.value = 0;
} else if (randomMove == "Paper") {
randomMove.value = 1;
} else if (randomMove == "Scissors") {
randomMove.value = 2;
}
return randomMove;
}
console.log(computerPlay());
var playerSelection = prompt("pick rock paper or scissors");
function userPlay() {
if (playerSelection == "rock") {
playerSelection.value = 0;
} else if (playerSelection == "paper") {
playerSelection.value = 1;
} else if (playerSelection == "scissors") {
playerSelection.value = 2;
}
return playerSelection;
}
console.log(userPlay());
function playRound(playerPick, computerSelection) {
if (playerPick == 0 && computerSelection == 2) {
alert("you win!!!");
} else if (playerPick == 0 && computerSelection == 1) {
alert("you lose hahahahaha!!!");
} else if (playerPick == computerSelection) {
alert("its a tie");
}
}
const playerPick = userPlay();
const computerSelection = computerPlay();
console.log(playRound(playerPick, computerSelection));
You shouldn't run it in console.log, as it doesn't have a return keyword, so it will return undefined. Just run it normally and it will be fine.
You are close. I cleaned it up a bit, added a function that maps variants of human input to a normalized value, and replaced the (incomplete) conditions in playRound with an object that captures all of the win conditions in an object (see beats)...
function normalizePlay(play) {
const variants = { r: 'rock', rock: 'rock', p: 'paper', paper: 'paper', s: 'scissors', scissors: 'scissors' };
return variants [play.toLowerCase()];
}
function computerPlay() {
var pickRandom = ["rock", "paper", "scissors"];
return pickRandom[Math.floor(Math.random() * 3)];
}
function playRound(human, computer) {
const beats = { rock: 'scissors', scissors: 'paper', paper: 'rock' }
let result;
if (human === computer) {
result = 'tie';
}
else if (beats[human] === computer) {
result = 'you win';
}
else result = 'computer wins';
console.log(`you picked ${human}, computer picked ${computer}... ${result}`)
}
const human = normalizePlay(prompt("pick rock paper or scissors (or r, p, s)"));
const computer = computerPlay();
playRound(human, computer);
function computerPlay() {
var pickRandom = ["Rock", "Paper", "Scissors"];
var randomMove = pickRandom[Math.floor(Math.random() * 3)];
if (randomMove == "Rock") {
return 0;
} else if (randomMove == "Paper") {
return 1;
} else if (randomMove == "Scissors") {
return 2;
}
}
var playerSelection = prompt("pick Rock, Paper or Scissors");
function userPlay() {
if (playerSelection == "Rock") {
return 0;
} else if (playerSelection == "Paper") {
return 1;
} else if (playerSelection == "Scissors") {
return 2;
}
}
function playRound(playerPick, computerSelection) {
console.log("player", playerPick)
console.log("computerSelection", computerSelection)
if (playerPick == 0 && computerSelection == 2) {
console.log("you win!!!");
} else if (playerPick == 0 && computerSelection == 1) {
console.log("you lose hahahahaha!!!");
} else if (playerPick == computerSelection) {
console.log("its a tie");
}
}
const playerPick = userPlay();
const computerSelection = computerPlay();
playRound(playerPick, computerSelection);
You you just needed to return the numbers from computerPlay and userPlay. Also, be careful with case sensitivity when using the prompt.
Currently stuck with a rock, paper, scissors game in javascript. Only my last function game() which needs to play a round 5 times is not working. Have tried to work with a for loop but it seems that I'm getting 5 times the same answer instead of 5 different random ones.
Could someone please help me out?
let playerScore = 0;
let computerScore = 0;
const playerSelection = playerPlay();
const computerSelection = computerPlay();
// computer select function
function computerPlay() {
const option = ['rock', 'paper', 'scissors'];
let random = Math.floor(Math.random() * 3);
return option[random];
}
console.log('Computer: ' + computerSelection);
// player select function
function playerPlay() {
const input = prompt('Please enter input');
const option = input.toLowerCase();
return option;
}
console.log('Player: ' + playerSelection);
// play 1 single round
function playRound(playerSelection, computerSelection) {
if(playerSelection === computerSelection) {
return 'It is a tie';
}
if(playerSelection === 'rock') {
if(computerSelection === 'scissors') {
playerScore++;
return 'Player wins with rock';
} else if(computerSelection === 'paper') {
computerScore++;
return 'Computer wins with paper'
}
}
if(playerSelection === 'paper') {
if(computerSelection === 'rock') {
playerScore++;
return 'Player wins with paper';
} else if(computerSelection === 'scissors') {
computerScore++;
return 'Computer wins with scissors';
}
}
if(playerSelection === 'scissors') {
if(computerSelection === "paper") {
playerScore++;
return 'Player wins with scissors';
} else if(computerSelection === 'rock') {
computerScore++;
return 'Computer wins with rock';
}
}
}
// console.log(playRound(playerSelection, computerSelection));
playRound(playerSelection, computerSelection);
console.log('Player: ' + playerScore);
console.log('Computer: ' + computerScore);
// game
function game() {
for(var i = 1; i <= 5; i++) {
console.log('repeat 5 times');
playRound(playerSelection, computerSelection);
}
}
game();
You have put a loop without asking for the player and computer input, hence it runs 5 times without taking any input. I have fixed this in the snippet. Not entirely sure if you want to run it this way though.
let playerScore = 0;
let computerScore = 0;
// computer select function
function computerPlay() {
const option = ['rock', 'paper', 'scissors'];
let random = Math.floor(Math.random() * 3);
return option[random];
}
// player select function
function playerPlay() {
const input = prompt('Please enter input');
const option = input.toLowerCase();
return option;
}
// play 1 single round
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return 'It is a tie';
}
if (playerSelection === 'rock') {
if (computerSelection === 'scissors') {
playerScore++;
return 'Player wins with rock';
} else if (computerSelection === 'paper') {
computerScore++;
return 'Computer wins with paper'
}
}
if (playerSelection === 'paper') {
if (computerSelection === 'rock') {
playerScore++;
return 'Player wins with paper';
} else if (computerSelection === 'scissors') {
computerScore++;
return 'Computer wins with scissors';
}
}
if (playerSelection === 'scissors') {
if (computerSelection === "paper") {
playerScore++;
return 'Player wins with scissors';
} else if (computerSelection === 'rock') {
computerScore++;
return 'Computer wins with rock';
}
}
}
// console.log(playRound(playerSelection, computerSelection));
// game
function game() {
for (i = 0; i <= 5; i++) {
var playerSelection = playerPlay();
var computerSelection = computerPlay();
playRound(playerSelection, computerSelection);
console.log('Computer: ' + computerSelection);
console.log('Player: ' + playerSelection);
console.log('Player: ' + playerScore);
console.log('Computer: ' + computerScore);
}
console.log('Final Player: ' + playerScore);
console.log('Final Computer: ' + computerScore);
}
game();
Your probleme is because you're set your playerSelection and computerSelection once (and even on a const ! So choice cannot be updated)
You just have to move this part into you for loop (and update to let instead of const)
let playerSelection = playerPlay();
let computerSelection = computerPlay();
let playerScore = 0;
let computerScore = 0;
// computer select function
function computerPlay() {
const option = ['rock', 'paper', 'scissors'];
let random = Math.floor(Math.random() * 3);
return option[random];
}
// player select function
function playerPlay() {
const input = prompt('Please enter input');
const option = input.toLowerCase();
return option;
}
// play 1 single round
function playRound(playerSelection, computerSelection) {
if(playerSelection === computerSelection) {
return 'It is a tie';
}
if(playerSelection === 'rock') {
if(computerSelection === 'scissors') {
playerScore++;
return 'Player wins with rock';
} else if(computerSelection === 'paper') {
computerScore++;
return 'Computer wins with paper'
}
}
if(playerSelection === 'paper') {
if(computerSelection === 'rock') {
playerScore++;
return 'Player wins with paper';
} else if(computerSelection === 'scissors') {
computerScore++;
return 'Computer wins with scissors';
}
}
if(playerSelection === 'scissors') {
if(computerSelection === "paper") {
playerScore++;
return 'Player wins with scissors';
} else if(computerSelection === 'rock') {
computerScore++;
return 'Computer wins with rock';
}
}
}
// game
function game() {
for(var i = 1; i <= 5; i++) {
let playerSelection = playerPlay();
let computerSelection = computerPlay();
console.log(`[Play turn ${i}] Player: ${playerSelection} | Computer: ${computerSelection}`);
playRound(i);
}
}
game();
Your code has a few issues.
You don't display output of next games. They are played but result is the same. It's same game configuration repeated 5 times.
console.log('repeat 5 times');
console.log(playRound(playerSelection, computerSelection));
Instead of:
console.log('repeat 5 times');
console.log(playRound(playerSelection, computerSelection));
You run functions playerSelection, computerSelection only once. So every next play has the same result.
You should execute these functions with every loop iteration.
Example:
let playerSelection = function () {
const input = prompt('Please enter input');
const option = input.toLowerCase();
return option;
};
let computerSelection = function () {
const option = ['rock', 'paper', 'scissors'];
let random = Math.floor(Math.random() * 3);
return option[random];
};
[...]
playRound(playerSelection(), computerSelection());