'Rock, paper, scissors - beginner in javascript
A month ago I started The Odin project, so please keep in mind the fact that I'm a beginner in this "world" of programming.
I'm currently stuck in a Rock, Paper, Scissors program and I thought you guys could enlighten me on what I'm doing wrong. I'll do my best to explain each step so you guys can follow my reasoning and better understand my code. I apologize if my explanation is not the best neither if I don't use the correct terms, as I said, I'm quite new in this world.
So, first I created a function that will randomly choose between rock, paper or scissors and attribute that value to the computerPlay variable.(It'll be the "computer's" choice between rock paper and scissors)
Secondly, pretty much the same function that I created for the computer, but this one is not random as it asks input to the player/user.
Then I created another function that includes both of the previous functions, which will dictate who wins between the player and the computer and also will update the score according to who wins.
And lastly I created another function that is supposed to execute 5 times the rock paper scissors game, but it's not working at all, it should be asking for the player's input 5 different times, but it doesn't, therefore, it gives the same output in those 5 times that the program executes.
Each time the program executes I'd like the player to be asked for new input and the computer to get assigned a random value again, that's why I wrote the computerSelection and playerSelection inside the loop.
Once again guys, sorry if my explanation was not the best or if my code is confusing, thank you for your time.
function computerPlay(){
let computerTurn = ["ROCK","PAPER", "SCISSORS"][Math.floor(Math.random() * 3)];
return computerTurn;
}
function playerPlay(){
let playerTurn = prompt("Choose between rock, paper or scissors: ");
playerTurn = playerTurn.toUpperCase();
return playerTurn;
}
let computerSelecion = computerPlay();
let playerSelection = playerPlay();
let win = "You win", tie = "That's a draw", lose = "You lost";
let playerScore = 0;
let computerScore = 0;
function playRound(playerSelection,computerSelecion){
if(playerSelection == "ROCK"){
if(computerSelecion == "ROCK"){
return tie;
}else if(computerSelecion == "PAPER"){
computerScore++;
return lose;
}else{
playerScore++;
return win;
}
}
if(playerSelection == "PAPER"){
if(computerSelecion == "ROCK"){
playerScore++;
return win;
}else if(computerSelecion == "PAPER"){
return tie;
}else{
computerScore++;
return lose;
}
}
if(playerSelection == "SCISSOR"){
if(computerSelecion == "ROCK"){
computerScore++;
return lose;
}else if(computerSelecion == "PAPER"){
playerScore++;
return win;
}else{
return tie;
}
}
}
function game(){
for(let i = 0; i < 5; i++){
playerSelection;
computerSelecion;
console.log(playRound(playerSelection, computerSelecion));
console.log("Player score: " + playerScore);
console.log("Computer's score: " + computerScore);
}
}
game();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
