JS CLI RPS game issue - javascript

Greetings to the caring people of Stack Overflow.
I just recently started to learn JS and am now trying to make a CLI Rock Paper Scissors game. Yes, I know that I am the 100th person who has asked questions about it, but I could not find the 'no-make-sense' code through the code of other people. So just I ask for help from everyone who is not indifferent to helping a newbie!
The situation is this: the most common rules of a familiar game, but I can't get the counter to work, plus sometimes the text I type in is not recognized (I think the reason for the input comparison function).
Also, I'm sure that my func scopes are broken but where and why I can't figure out or trace it.
function computerPlay() {
let anyPick = Math.random();
if (anyPick < 0.3) {
return "Rock";
}
if (anyPick > 0.3 && anyPick < 0.6) {
return "Paper";
}
if (anyPick > 0.6) {
return "Scissors";
}
}
let aiScore = 0;
let playerScore = 0;
function battleRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return "None of you are win or lose, cuz it is an equal!";
}
if (playerSelection === "rock" && computerSelection === "Scissors") {
playerScore += 1;
return "You Win! Rock beats Scissors";
}
if (playerSelection === "rock" && computerSelection === "Paper") {
aiScore += 1;
return "You Loose! Paper beats Rock";
}
if (playerSelection === "paper" && computerSelection === "Scissors") {
aiScore += 1;
return "You Loose! Scissors cut Paper";
}
if (playerSelection === "paper" && computerSelection === "Rock") {
playerScore += 1;
return "You Win! Paper beats Rock";
}
if (playerSelection === "scissors" && computerSelection === "Paper") {
playerScore += 1;
return "You Win! Scissors cut Paper";
}
if (playerSelection === "scissors" && computerSelection === "Rock") {
aiScore += 1;
return "You Loose! Rock beats Scissors";
} else return "U misspelled, try again";
}
function scores(aiScore, playerScore) {
if (aiScore > playerScore) {
return "CONGRAS, AI IS SMARTER THEN YOU!";
}
if (playerScore > aiScore) {
return "CONGRAS, YOU ARE SMARTER THEN AI";
}
}
for (let i = 0; i < 5; i++) {
const playerSelection = prompt(
"Choose: Rock, Paper or Scissors",
" "
).toLowerCase();
const computerSelection = computerPlay();
console.log("AI choose: " + computerSelection);
console.log(battleRound(playerSelection, computerSelection));
console.log("AI score: " + aiScore, "Player score: " + playerScore);
}
Please, indicate an obvious problem in the code and what exactly I need to replace.
Much obliged for any tips!

There are two main problems with your code that might be causing this issue,
You are setting the default prompt value to an empty space. By using:
prompt("Choose: Rock, Paper or Scissors", " ")
The default prompt value is an empty space, making it easier to mistype the value.
To fix this, I removed the default value and added the .trim() method to remove whitespace from the start and end of the input.
You are making impossible comparisons. In your code you made the comparison:
if (playerSelection === computerSelection) {
return ("None of you are win or lose, cuz it is an equal!");
}
This condition will never be true, as the playerSelection is always converted to lowercase e.g "rock", but the computerSelection's first letter is always uppercase e.g "Rock".
I added the .toLowerCase() method to the end of computerSelection before comparing.
Full code:
function computerPlay() {
let anyPick = Math.random();
if (anyPick < 0.3) {
return "Rock";
}
if (anyPick > 0.3 && anyPick < 0.6) {
return "Paper";
}
if (anyPick > 0.6) {
return "Scissors";
}
}
let aiScore = 0;
let playerScore = 0;
function battleRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection.toLowerCase()) {
return "None of you are win or lose, cuz it is an equal!";
}
if (playerSelection === "rock" && computerSelection === "Scissors") {
playerScore += 1;
return "You Win! Rock beats Scissors";
}
if (playerSelection === "rock" && computerSelection === "Paper") {
aiScore += 1;
return "You Loose! Paper beats Rock";
}
if (playerSelection === "paper" && computerSelection === "Scissors") {
aiScore += 1;
return "You Loose! Scissors cut Paper";
}
if (playerSelection === "paper" && computerSelection === "Rock") {
playerScore += 1;
return "You Win! Paper beats Rock";
}
if (playerSelection === "scissors" && computerSelection === "Paper") {
playerScore += 1;
return "You Win! Scissors cut Paper";
}
if (playerSelection === "scissors" && computerSelection === "Rock") {
aiScore += 1;
return "You Loose! Rock beats Scissors";
} else return "U misspelled, try again";
}
function finalScores() {
if (aiScore > playerScore) {
return "CONGRATS, AI IS SMARTER THEN YOU!";
}
if (playerScore > aiScore) {
return "CONGRATS, YOU ARE SMARTER THEN AI";
}
}
for (let i = 0; i < 5; i++) {
const playerSelection = (prompt("Choose: Rock, Paper or Scissors") ?? "")
.trim()
.toLowerCase();
const computerSelection = computerPlay();
console.log("AI choose: " + computerSelection);
console.log(battleRound(playerSelection, computerSelection));
console.log("AI score: " + aiScore, "Player score: " + playerScore);
}
console.log(finalScores())

Related

Loop prompt if a value is empty / wrong?

I'm doing a simple rock, paper, exercise on console (from The Odin Project) and I am stucked in some point. I want the prompt that selects rock, paper or scissors to keep showing if the user input is empty or wrong (if is a different string from the variable choices) and I'm not getting the reuslt I want, the alert shows, yes, but it shows whether if you input a correct value, a wrong value or if you left it empty and the prompt also finishes after the loop for ends which I only want to finish if the input is "rock", "paper" or "scissors".
Can someone enlight me here? I'm just blank.
const choices = ["rock", "paper", "scissors"];
let playerScore = 0;
let computerScore = 0;
let playerName = "";
function getComputerChoice() {
let random = choices[Math.floor(Math.random() * choices.length)];
console.log(random);
return random;
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return "It's a tie";
} else if (playerSelection === "rock") {
if (computerSelection === "scissors") {
playerScore++;
return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
} else if (computerSelection === "paper") {
computerScore++;
return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
}
} else if (playerSelection === "paper") {
if (computerSelection === "rock") {
playerScore++;
return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
} else if (computerSelection === "scissors") {
computerScore++;
return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
}
} else if (playerSelection === "scissors") {
if (computerSelection === "paper") {
playerScore++;
return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
} else if (computerSelection === "rock") {
computerScore++;
return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
}
}
}
function playerChoice() {
const userChoice = prompt("Rock, paper, scissors").toLowerCase();
if (userChoice == null || userChoice !== choices) {
alert("Empty or wrong choice!");
} else if (choices.includes(userChoice)) {
return userChoice;
}
}
function getPlayerName() {
playerName = prompt("What is your name?");
return playerName;
}
function game () {
getPlayerName();
console.log(`Welcome ${playerName}`)
for (i = 0; i < 3; i++) {
const computerSelection = getComputerChoice();
const playerSelection = playerChoice();
console.log(playRound(playerSelection, computerSelection));
}
if (playerScore > computerScore) {
return "Game over! Player wins!";
} else if (playerScore < computerScore) {
return "Game over! Player loses!";
} else {
return "Game over, draw!";
}
}
console.log(game());
Thank you very much.
The problem is you're checking whether the input is the same as the array choices. using the include function here will check that the user's choice is in the array
const choices = ["rock", "paper", "scissors"];
let playerScore = 0;
let computerScore = 0;
let playerName = "";
function getComputerChoice() {
let random = choices[Math.floor(Math.random() * choices.length)];
console.log(random);
return random;
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return "It's a tie";
} else if (playerSelection === "rock") {
if (computerSelection === "scissors") {
playerScore++;
return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
} else if (computerSelection === "paper") {
computerScore++;
return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
}
} else if (playerSelection === "paper") {
if (computerSelection === "rock") {
playerScore++;
return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
} else if (computerSelection === "scissors") {
computerScore++;
return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
}
} else if (playerSelection === "scissors") {
if (computerSelection === "paper") {
playerScore++;
return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
} else if (computerSelection === "rock") {
computerScore++;
return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
}
}
}
function playerChoice() {
const userChoice = prompt("Rock, paper, scissors").toLowerCase();
console.log(choices.includes(userChoice))
if (userChoice == null || !choices.includes(userChoice)) {
alert("Empty or wrong choice!");
} else if (choices.includes(userChoice)) {
return userChoice;
}
}
function getPlayerName() {
playerName = prompt("What is your name?");
return playerName;
}
function game () {
getPlayerName();
console.log(`Welcome ${playerName}`)
for (i = 0; i < 3; i++) {
const computerSelection = getComputerChoice();
const playerSelection = playerChoice();
console.log(playRound(playerSelection, computerSelection));
}
if (playerScore > computerScore) {
return "Game over! Player wins!";
} else if (playerScore < computerScore) {
return "Game over! Player loses!";
} else {
return "Game over, draw!";
}
}
console.log(game());
Edit: Also you might want to change this line as a null input won't have the attribute .toLowerCase()
const userChoice = prompt("Rock, paper, scissors").toLowerCase();

Stuck on Rock, Paper, Scissors

I'm working through my first JS project and making a basic Rock, Paper, Scissors game. I get stuck when I attempt to play a single round. I don't know if my code accepts case-insensitive input from the user (rock, Rock, ROCK, rOcK).
My console seems to display a random result of the game. For example, if I input Rock, and the computer also inputs Rock, the console sometimes displays "You win!", and I don't know why that's happening.
Here is my code below. Any guidance is greatly appreciated!
function getComputerChoice() {
const randomNum = Math.floor(Math.random() * 3)
switch (randomNum) {
case 0:
return "rock";
case 1:
return "paper";
case 2:
return "scissors";
}
}
console.log(getComputerChoice());
function userInput() {
const ask = prompt("To begin game, select Rock, Paper, or Scissors");
return ask.toLowerCase();
}
function playRound(playerSelection, computerSelection) {
if (playerSelection == "rock" && computerSelection == "scissors") {
return "You win!";
} else if (playerSelection == "rock" && computerSelection == "paper") {
return "You lose";
} else if (playerSelection == "scissors" && computerSelection == "paper") {
return "You win!";
} else if (playerSelection == "scissors" && computerSelection == "rock") {
return "You lose";
} else if (playerSelection == "paper" && computerSelection == "rock") {
return "You win!";
} else if (playerSelection == "paper" && computerSelection == "scissors") {
return "You lose";
} else if (playerSelection == computerSelection) {
return "It's a tie";
} else {
return "Try again";
}
}
const playerSelection = userInput();
const computerSelection = getComputerChoice();
console.log(playRound(playerSelection, computerSelection));
It looks like you are displaying a random computer choice, then selecting again for the game. To fix this remove line 12 (console.log(getComputerChoice());) and add the following line right after you declare computerSelection:
console.log(computerSelection);
Here is the final code:
function getComputerChoice() {
const randomNum = Math.floor(Math.random() * 3)
switch (randomNum) {
case 0:
return "rock";
case 1:
return "paper";
case 2:
return "scissors";
}
}
function userInput() {
const ask = prompt("To begin game, select Rock, Paper, or Scissors");
return ask.toLowerCase();
}
function playRound(playerSelection, computerSelection) {
if (playerSelection == "rock" && computerSelection == "scissors") {
return "You win!";
} else if (playerSelection == "rock" && computerSelection == "paper") {
return "You lose";
} else if (playerSelection == "scissors" && computerSelection == "paper") {
return "You win!";
} else if (playerSelection == "scissors" && computerSelection == "rock") {
return "You lose";
} else if (playerSelection == "paper" && computerSelection == "rock") {
return "You win!";
} else if (playerSelection == "paper" && computerSelection == "scissors") {
return "You lose";
} else if (playerSelection == computerSelection) {
return "It's a tie";
} else {
return "Try again";
}
}
const playerSelection = userInput();
const computerSelection = getComputerChoice();
console.log(computerSelection);
console.log(playRound(playerSelection, computerSelection));

Scoreboard and determine winner Rock Paper Scissors (JavaScript)

I'm working on Rock Paper Scissors and I'm lost at how to keep and display scores as well as determine a winner when they reach a score of 5. Right now, I'm adding +1 to the playerScore or compScore depending on the single round result, but I don't know how to display or keep track of it. Here is my code so far. Any tips are greatly appreciated!
//choices array
const choices = ["rock", "paper", "scissors"];
//set scores to 0
let playerScore = 0;
let compScore = 0;
//Get random choice from comp
function getComputerChoice() {
const randomNum = Math.floor(Math.random() * 3) //Generate random num with Math.random(), to ensure it's between 0 and 3, multiply by 3. Use Math.floor to round to down to nearest num
switch (randomNum) { //Create switch statement to take randomNum variable to perform different action based on the condition (case)
case 0:
return "rock";
case 1:
return "paper";
case 2:
return "scissors";
}
}
//single round gameplay
function playRound(playerSelection, computerSelection) {
if (playerSelection == "rock" && computerSelection == "scissors") {
//Add 1 to playerScore
playerScore+= 1
return "You win! Rock beats scissors";
} else if (playerSelection == "rock" && computerSelection == "paper") {
//Add 1 to compScore
compScore+= 1
return "You lose. Paper beats rock";
} else if (playerSelection == "scissors" && computerSelection == "paper") {
playerScore+= 1
return "You win! Scissors beat paper";
} else if (playerSelection == "scissors" && computerSelection == "rock") {
compScore+= 1
return "You lose. Rock beats scissors";
} else if (playerSelection == "paper" && computerSelection == "rock") {
playerScore+= 1
return "You win! Paper beats rock";
} else if (playerSelection == "paper" && computerSelection == "scissors") {
compScore+= 1
return "You lose. Scissors beat paper";
} else if (playerSelection === computerSelection) {
return "It's a tie";
} else {
return "Try again";
}
}
function userInput() {
let ask = false;
//while ask is still false, continue looping
while (ask == false){
const selction = prompt("Rock Paper or Scissors?")
// if the prompt return is empty
if (selction == null){
//keep looping
continue;
}
const selctionLower = selction.toLowerCase();
//ask if array of choices [rock, paper, scissors] is in the user input
if (choices.includes(selctionLower)){
//then ask is true
ask = true;
return selctionLower;
}
}
}
function game() {
//loop
for (let i = 0; i < 5; i++) {
const playerSelection = userInput();
const computerSelection = getComputerChoice();
//call playRound function
console.log(playRound(playerSelection, computerSelection));
}
}
game()

Javascript output in alert is different in console, and counter is not working properly even when condition is met

I'm making a rock paper scissors games where the counter should add 1 if user wins, add 0.5 if it's a tie, and remains the same if user lost. But in the alert and what's shown in the console doesn't match. Sometimes its shown in the alert that I win and in the console its shown I lose or its a tie. And the counter doesn't add up correctly.
In the image above it's shown 3 wins, 1 tie, and 1 lose. It should show I've won 3.5 games but it shows 5.5. And there is only 5 games played so it should be impossible to win 5.5.
Here is the code:
let winCounter = 0;
// playRound function will play a game of rock, paper, scissors and returns the result
function playRound(playerSelection, computerSelection) {
let getPlayerInsensitive = playerSelection.toLowerCase();
if (getPlayerInsensitive === "rock" && computerSelection === "Rock") {
winCounter+=0.5;
return "It's a tie, both are Rock";
} else if (getPlayerInsensitive === "rock" && computerSelection === "Paper") {
winCounter = winCounter;
return "You Lose! Paper beats Rock";
} else if (getPlayerInsensitive === "rock" && computerSelection === "Scissors") {
winCounter+=1;
return "You win! Rock beats Scissors";
} else if (getPlayerInsensitive === "paper" && computerSelection === "Rock") {
winCounter+=1;
return "You win! Paper beats Rock";
} else if (getPlayerInsensitive === "paper" && computerSelection === "Paper") {
winCounter+=0.5;
return "It's a tie! Both are Paper";
} else if (getPlayerInsensitive === "paper" && computerSelection === "Scissors") {
winCounter = winCounter;
return "You lose! Scissors beats Paper";
} else if (getPlayerInsensitive === "scissors" && computerSelection === "Rock") {
winCounter = winCounter;
return "You lose! Rock beats Scissors";
} else if (getPlayerInsensitive === "scissors" && computerSelection === "Scissors") {
winCounter=0.5;
return "It's a tie! Both are Scissors";
} else if (getPlayerInsensitive === "scissors" && computerSelection === "Paper") {
winCounter+=1;
return "You win! Scissors beat Paper";
} else {
return "Check your spelling!";
}
}
// game function returns the winner after five rounds
function game() {
for (let i = 0; i < 5; i++) {
let getSelect = prompt("Choose Rock, Paper, or Scissors", "");
if (getSelect === null || getSelect === "") {
alert("You clicked Cancel!");
}
alert(playRound(getSelect, computerPlay()));
if (i < 5) {
console.log(playRound(getSelect, computerPlay()));
}
//outputs the winner of 5 games
if (i === 4) {
alert("You've played 5 games");
if (winCounter >= 3) {
alert(`You've won ${winCounter} out of 5 games. You win!`);
return `You've won ${winCounter} out of 5 games. You win!`;
} else if (winCounter === 2.5) {
alert(`You've won 2.5 out of 5 games. It's a tie!`);
return `You've won 2.5 out of 5 games. It's a tie!`;
} else if (winCounter < 2.5) {
alert(`You've won ${winCounter} out of 5 games. You lost!`);
return `You've won ${winCounter} out of 5 games. You lost!`;
}
}
}
}
console.log(game());
And here is the fiddle: https://jsfiddle.net/JaredDev/o62hr7as/125/
Why does it show different results in alert and console.log
It's because you called the computerPlay function twice, thus could returns different selection each time it is called,
alert(playRound(getSelect, computerPlay()));
if (i < 5) {
console.log(playRound(getSelect, computerPlay()));
}
you could change it to this, so it is called only once
const result = playRound(getSelect, computerPlay())
alert(result);
if (i < 5) {
console.log(result)
}
also why the score could be more than it should have, its because playRound function also called twice each turn
Caling code with random behaviour and side-effects twice
This piece of code produces an unintended result because the function computerPlay has random behaviour and the function playRound has has side-effects (modifies the winCounter).
alert(playRound(getSelect, computerPlay()));
if (i < 5) {
console.log(playRound(getSelect, computerPlay()));
}
You could correct this part of the problem by calling the function only once and assigning its result to a variable:
const result = playRound(getSelect, computerPlay());
alert(result);
if (i < 5) {
console.log(result);
}
Typo
You also have a typo:
} else if (getPlayerInsensitive === "scissors" && computerSelection === "Scissors") {
winCounter=0.5;
return "It's a tie! Both are Scissors";
This should say += not just =:
winCounter+=0.5;

I can't assign a function of random numbers to appear when I enter the variable I set it as

I have created the below code so that the computer plays 3 different random outcomes
function computerPlay() {
let computerRandomNumber = Math.floor(Math.random() * 3)
if (computerRandomNumber === 0) {
return "rock";
} else if (computerRandomNumber === 1) {
return "paper";
} else(computerRandomNumber === 2)
return "scissors";
}
So far so good!, then I want to run another function where there will be player VS computer
function playRound() {
if (playerSelection === "rock" && computerSelection === "rock") {
return "It's a tie! Ties are lame and so are you for tying."
} else if (playerSelection === "rock" && computerSelection === "paper") {
return "Loooooser looooser nia nia nia! Paper wrapped your rock and threw it in the bin!"
} etc. etc.
I have declared these two variables for player and computer so that the above function works.
let playerSelection = ["rock", "paper", "scissors"];
let computerSelection = computerPlay();
When I console.log(computerSelection), I always get the same result and no random values, but when I console log computerPlay() it works just fine and I am getting random outcomes. Why is this happening to me? :(
Below is the whole code I have written so far
function computerPlay() {
let computerRandomNumber = Math.floor(Math.random() * 3)
if (computerRandomNumber === 0) {
return "rock";
} else if (computerRandomNumber === 1) {
return "paper";
} else(computerRandomNumber === 2)
return "scissors";
}
let playerSelection = ["rock", "paper", "scissors"];
let computerSelection = computerPlay();
function playRound() {
if (playerSelection === "rock" && computerSelection === "rock") {
return "It's a tie! Ties are lame and so are you for tying."
} else if (playerSelection === "rock" && computerSelection === "paper") {
return "Loooooser looooser nia nia nia! Paper wrapped your rock and threw it in the bin!"
} else if (playerSelection === "rock" && computerSelection === "scissors") {
return "Bravoooo you made it! Rock smashed the scissors! "
} else if (playerSelection === "paper" && computerSelection === "rock") {
return "Bravoooo you made it! Paper wrapped the rock and threw it in the bin! "
} else if (playerSelection === "paper" && computerSelection === "paper") {
return "It's a tie! Ties are lame and so are you for tying. "
} else if (playerSelection === "paper" && computerSelection === "scissors") {
return "Loooooser looooser nia nia nia! Scissors cut through your paper and made it look like an ugly origami swan"
} else if (playerSelection === "scissors" && computerSelection === "rock") {
return "Loooooser looooser nia nia nia! Rock smashed the scissors. "
} else if (playerSelection === "scissors" && computerSelection === "paper") {
return "Bravoooo you made it! Scissors cut through paper and made it look like an ugly origami swan"
} else(playerSelection === "scissors" && computerSelection === "scissors")
return "It's a tie! Ties are lame and so are you for tying. "
}
You assign a variable once and never change it.
if you want the computer to make a decision on each round, you could write something like this:
function computerPlay() {
let computerRandomNumber = Math.floor(Math.random() * 3)
if (computerRandomNumber === 0) {
return "rock";
} else if (computerRandomNumber === 1) {
return "paper";
} else {
return "scissors";
}
}
let playerSelectionVariants = ["rock", "paper", "scissors"];
function playRound() {
let computerSelection = computerPlay();
let playerSelection = /* request user input from playerSelectionVariants */
if (playerSelection === "rock" && computerSelection === "rock") {
return "It's a tie! Ties are lame and so are you for tying."
} else if (playerSelection === "rock" && computerSelection === "paper") {
return "Loooooser looooser nia nia nia! Paper wrapped your rock and threw it in the bin!"
} else if (playerSelection === "rock" && computerSelection === "scissors") {
return "Bravoooo you made it! Rock smashed the scissors! "
} else if (playerSelection === "paper" && computerSelection === "rock") {
return "Bravoooo you made it! Paper wrapped the rock and threw it in the bin! "
} else if (playerSelection === "paper" && computerSelection === "paper") {
return "It's a tie! Ties are lame and so are you for tying. "
} else if (playerSelection === "paper" && computerSelection === "scissors") {
return "Loooooser looooser nia nia nia! Scissors cut through your paper and made it look like an ugly origami swan"
} else if (playerSelection === "scissors" && computerSelection === "rock") {
return "Loooooser looooser nia nia nia! Rock smashed the scissors. "
} else if (playerSelection === "scissors" && computerSelection === "paper") {
return "Bravoooo you made it! Scissors cut through paper and made it look like an ugly origami swan"
} else {
return "It's a tie! Ties are lame and so are you for tying. "
}
}

Categories