My code is not running but i have no errors - javascript

The project is about making a rock paper scissors game using functions and loops etc.
the code below is how the project looks
the first function is to get the randomized computer choice
the second function is to get the user or players choice
the third function is to play the game and check if the player won or the computer won
the final function is to create a for loop to run the third function a certain number of times to see who is the winner of the game
Been working on it since and have no idea why it doesn't work
```
function getComputerChoice() {
let values = ["rock", "paper", "scissors"];
return values[Math.floor(Math.random() * values.length)];
}
function getPlayerChoice() {
let getChoice = "rock";
let value = getChoice.trim();
let lowCase = value.toLowerCase();
let capitalize = lowCase.charAt(0).toUpperCase + lowCase.slice(1);
while (!["Rock", "Paper", "Scissors"].includes(capitalize)) {
value = getChoice.trim();
lowCase = value.toLowerCase();
capitalize = lowCase.charAt(0).toUpperCase + lowCase.slice(1);
}
return capitalize;
}
function playRound(playerSelection, computerSelection) {
let games = "";
if (
(playerSelection == "rock" && computerSelection == "paper") ||
(playerSelection == "paper" && computerSelection == "scissors") ||
(playerSelection == "scissors" && computerSelection == "rock")
) {
return (games =
"player loses!! " + computerSelection + " beats " + playerSelection);
} else if (
(playerSelection == "paper" && computerSelection == "rock") ||
(playerSelection == "scissors" && computerSelection == "paper") ||
(playerSelection == "rock" && computerSelection == "scissors")
) {
return (games =
"player Wins!! " + playerSelection + " beats " + computerSelection);
} else if (playerSelection == computerSelection) {
return (games =
"it a tie noice " + playerSelection + " v " + computerSelection);
} else {
return (games = "euphoria");
}
}
function game() {
let playerScores = 0;
let computerScores = 0;
let computerSelection = "";
let playerSelection = "";
computerSelection = getComputerChoice();
playerSelection = getPlayerChoice();
for (let i = 0; i < 5; i++) {
if (
playRound(
"player loses!! " + computerSelection + " beats " + playerSelection
)
) {
computerScores += 1;
console.log(
"you lost this round!! boo hoo!! scores are " +
computerScores +
" v " +
playerScores
);
} else if (
playRound(
"player Wins!! " + playerSelection + " beats " + computerSelection
)
) {
playerScores += 1;
console.log(
"you Won this round!! hurray!! scores are " +
computerScores +
" v " +
playerScores
);
}
}
if (playerScores > computerScores) {
console.log("congratulations you won this round");
} else if (playerScores < computerScores) {
console.log("im sorry you lost this round");
} else {
console.log("there is a problem");
}
}
game();
```

It doesn't seem that playRound() is returning anything:
function playRound(playerSelection, computerSelection) {
let games = '';
if (
(playerSelection == "rock" && computerSelection == "paper") ||
(playerSelection == "paper" && computerSelection == "scissors") ||
(playerSelection == "scissors" && computerSelection == "rock")
) {
games = "player loses!! " + computerSelection + " beats " + playerSelection;
// ADD RETURN HERE
} else if (
(playerSelection == "paper" && computerSelection == "rock") ||
(playerSelection == "scissors" && computerSelection == "paper") ||
(playerSelection == "rock" && computerSelection == "scissors")
) {
games = "player Wins!! " + playerSelection + " beats " + computerSelection;
// ADD RETURN HERE
} else if (playerSelection == computerSelection) {
games = "it a tie noice " + playerSelection + " v " + computerSelection;
// ADD RETURN HERE
} else {
games = "euphoria";
// ADD RETURN HERE
}
}
Explanation
In your game() function definition you are checking if(playRound()). playRound is not returning anything so this is interpreted as a void function, hence, the if will always evaluate to false.

Related

Can anyone tell me why my code doesn't work properly? Game ROCK, PAPER, SCISSORS. Java Script

Can anyone tell me why my code doesn't work properly? I'm trying to make game ROCK, PAPER, SCISSORS on plain Java Script. For some reason it doesn't work as I expect.
const computerAanswer = ["rock", "paper", "scissors"];
function computerPlay() {
let answer = computerAanswer[Math.floor(Math.random() * computerAanswer.length)];
return answer;
}
console.log('computer choice is: ' + computerPlay().toUpperCase());
function playRound (playerSelection, computerSelection) {
playerSelection = playerSelection.toLowerCase()
if (playerSelection == "rock" && computerSelection == "scissors") {
return "Congrats,you are winner!";
} else if (playerSelection == "paper" && computerSelection == "rock") {
return "Congrats,you are winner!";
} else if (playerSelection == "scissors" && computerSelection == "paper") {
return "Congrats,you are winner!";
} else if (playerSelection == "rock" && computerSelection == "rock") {
return "Draw!";
} else if (playerSelection == "paper" && computerSelection == "paper") {
return "Draw!";
} else if (playerSelection == "scissors" && computerSelection == "scissors") {
return "Draw!";
} else {
return "You lose. Maybe next time...";
}
}
let playerSelection = prompt("Make your choose: Rock, Paper or Scissors?");
let computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
console.log('player choice is: ' + playerSelection.toUpperCase());
I guess that's just your first console.log :
console.log('computer choice is: ' + computerPlay().toUpperCase());
It plays a round for computer, then you play another one against prompted user.
Do that instead :
function computerPlay() {
let answer = computerAanswer[Math.floor(Math.random() * computerAanswer.length)];
console.log('computer choice is: ' + answer.toUpperCase());
return answer;
}
When I nested
console.log('computer choice is: ' + answer.toUpperCase());
into computerPlay function it works.
const computerAanswer = ["rock", "paper", "scissors"];
function computerPlay() {
let answer = computerAanswer[Math.floor(Math.random() * computerAanswer.length)];
console.log('computer choice is: ' + answer.toUpperCase());
return answer;
}
function playRound (playerSelection, computerSelection) {
playerSelection = playerSelection.toLowerCase()
if (playerSelection == "rock" && computerSelection == "scissors") {
return "Congrats,you are winner!";
} else if (playerSelection == "paper" && computerSelection == "rock") {
return "Congrats,you are winner!";
} else if (playerSelection == "scissors" && computerSelection == "paper") {
return "Congrats,you are winner!";
} else if (playerSelection == "rock" && computerSelection == "rock") {
return "Draw!";
} else if (playerSelection == "paper" && computerSelection == "paper") {
return "Draw!";
} else if (playerSelection == "scissors" && computerSelection == "scissors") {
return "Draw!";
} else {
return "You lose. Maybe next time...";
}
}
let playerSelection = prompt("Make your choose: Rock, Paper or Scissors?");
let computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
console.log('player choice is: ' + playerSelection.toUpperCase());

What can i do to made "Rock, papper, scissors" work?

i'm very new to js, and it's my first code on it. i'm trying to make a game "rock, papper, scissors". please, look at my code, my funcrion playRound doesn't wor, i need it to return scores.
please, give me advises how to change my code, but i don't want you to solve all for me. thank you.
function computerPlay() {
let computerAction = Math.floor(Math.random() * choises.length);
let computerSelection = choises[computerAction];
return computerSelection;
}
function userPlay() {
let playerSelection = prompt(choises).toLowerCase();
return playerSelection;
}
let choises = ['rock', 'paper', 'scissors'];
let Score = new Map([
['userScores', 0],
['computerScores', 0]
]);
function playRound(playerSelection, computerSelection) {
if (playerSelection === choises[0] && computerSelection === choises[0]) ||
(playerSelection === choises[1] && computerSelection === choises[1]) ||
(playerSelection === choises[2] && computerSelection === choises[2]){
return Score = ['userScores' + 1, 'computerScores' + 1];
} else if (playerSelection === choises[0] && computerSelection !== choises[0]) ||
(playerSelection === choises[1] && computerSelection !== choises[1]) ||
(playerSelection === choises[2] && computerSelection !== choises[2])
return Score = ['userScores' + 1, 'computerScores'];
} else if (playerSelection !== choises[0] && computerSelection === choises[0]) ||
(playerSelection !== choises[1] && computerSelection === choises[1]) ||
(playerSelection !== choises[2] && computerSelection === choises[2])
{
return Score = ['userScores', 'computerScores' + 1];
}
}
Refer to this:
function computerPlay() {
let computerAction = Math.floor(Math.random() * choises.length);
let computerSelection = choises[computerAction];
return computerSelection;
}
function userPlay() {
let playerSelection = prompt(choises).toLowerCase();
return playerSelection;
}
let choises = ['rock', 'paper', 'scissors'];
let Score = {
user: 0,
computer: 0
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection){
Score.user = Score.user + 1;
Score.computer = Score.computer + 1;
} else if ((playerSelection === choises[0] && computerSelection === choises[2]) ||
(playerSelection === choises[1] && computerSelection === choises[0]) ||
(playerSelection === choises[2] && computerSelection === choises[1])) {
Score.user = Score.user + 1;
} else {
Score.computer = Score.computer + 1;
}
}
let computerChoice = computerPlay();
let userChoice = userPlay();
playRound(userChoice, computerChoice);
console.log('computer hand', computerChoice);
console.log('user hand', userChoice);
console.log(Score);

Rock, Paper, Scissors Game shows both win and lose messages

I'm currently working through a Rock, Paper, Scissors project that plays out in the console (this is for The Odin Project, front end coming soon).
Here is what my script looks like for reference:
<script>
function computerPlay() {
const choice = ["Rock", "Paper", "Scissors"]
return choice[Math.floor(Math.random() * choice.length)]
}
function play(playerSelection, computerSelection) {
const lose = console.log('You lose! ' + computerSelection + ' beats ' + playerSelection + '!')
const win = console.log('You win! ' + playerSelection + ' beats ' + computerSelection + '!')
if (playerSelection === computerSelection) {
return console.log("It's a draw! Try again!")
}
if (playerSelection === "rock" && computerSelection === "Paper") {
return lose
}
if (playerSelection === "rock" && computerSelection === "Scissors") {
return win
}
if (playerSelection === "paper" && computerSelection === "Scissors") {
return lose
}
if (playerSelection === "paper" && computerSelection === "Rock") {
return win
}
if (playerSelection === "scissors" && computerSelection === "Rock") {
return lose
}
if (playerSelection === "scissors" && computerSelection === "Paper") {
return win
}
}
function game() {
playerSelect = prompt("Welcome to Rock, Paper, Scissors! Which one do you choose? \n")
compSelect = computerPlay()
console.log("Player chose " + playerSelect)
console.log("Computer chose " + compSelect)
console.log(play(playerSelect, compSelect))
}
</script>
Right now my output is showing both the win and lose conditions for any given choice like this:
Player chose rock
Computer chose Rock
You lose! Rock beats rock!
You win! rock beats Rock!
I chose to keep the win and lose messages in variables but I know I probably screwed some small detail up there. I've tried adding/removing the if else statements but both messages still show up no matter what choice.
I'm planning on making either choice case insensitive until I solve this error.
Any help would be appreciated, thanks!:
When you are calling your console.log at the beginning of the script, you are not saving them for later, you are running them and assigning their return value to a variable. What you could do is have a win and a lose variable that are function and then call them when the player is winning/loosing.
function computerPlay() {
const choice = ["Rock", "Paper", "Scissors"];
return choice[Math.floor(Math.random() * choice.length)];
}
function play(playerSelection, computerSelection) {
// we are storing a function into the win and lose variable.
const lose = () => console.log('You lose! ' + computerSelection + ' beats ' + playerSelection + '!');
const win = () => console.log('You win! ' + playerSelection + ' beats ' + computerSelection + '!');
if (playerSelection === computerSelection) {
return console.log("It's a draw! Try again!")
}
if (playerSelection === "rock" && computerSelection === "Paper") {
return lose()
}
if (playerSelection === "rock" && computerSelection === "Scissors") {
return win()
}
if (playerSelection === "paper" && computerSelection === "Scissors") {
return lose()
}
if (playerSelection === "paper" && computerSelection === "Rock") {
return win()
}
if (playerSelection === "scissors" && computerSelection === "Rock") {
return lose()
}
if (playerSelection === "scissors" && computerSelection === "Paper") {
return win()
}
}
function game() {
playerSelect = 'rock';
compSelect = computerPlay();
console.log("Player chose " + playerSelect);
console.log("Computer chose " + compSelect);
// You don't need to console.log the return value of a console.log
play(playerSelect, compSelect);
}
game();
Try changing your lose and win variables to their string values instead of console.log("/message/").
Your lose and win variable declarations are already printing their messages instead of storing them, since your code calls an instance of console.log().
That way, when you return either win or lose, your console.log(play(playerSelect, compSelect)) will receive a string, and then print it.

Looping basic Javascript game with a win condition

How can I loop my game of rock, paper, scissors and have it keep score with a win condition?
I have tried to create a "game" function that loops the rounds of RPS until the player or computer reaches a score of 5. However, I can't get the scores to stick, nor does the game loop.
playerSelection = prompt( ' Enter Rock, Paper, or Scissors');
let winner = 0;
let humanScore = 0;
let computerScore = 0;
function computerPlay() {
let number = Math.floor(Math.random() * (3 + 1));
if(number == 1)
return 'Rock';
else if(number == 2)
return 'Paper';
else return 'Scissors';
}
let computerSelection = computerPlay();
playerSelection = playerSelection.toUpperCase();
computerSelection = computerSelection.toUpperCase();
function game() {
while( humanScore <= 5 || computerScore <= 5) {
playRound();
}
}
function playRound(playerSelection, computerSelection) {
if( playerSelection === 'ROCK' && computerSelection === 'SCISSORS') {
humanScore +=1;
return playerSelection + ' wins vs ' + computerSelection + '. Congratulations!';
} else if( playerSelection === 'ROCK' && computerSelection === 'PAPER') {
computerScore +=2;
return playerSelection + ' loses vs ' + computerSelection + '. Try again!';
} else if( playerSelection === 'ROCK' && computerSelection === 'ROCK') {
return playerSelection + ' ties with ' + computerSelection + '. Try again!';
} else if( playerSelection === 'PAPER' && computerSelection === 'ROCK') {
humanScore +=1;
return playerSelection + ' wins vs ' + computerSelection + '. Congratulations!';
} else if( playerSelection === 'PAPER' && computerSelection === 'PAPER') {
return playerSelection + ' ties with ' + computerSelection + '. Try again!';
} else if( playerSelection === 'PAPER' && computerSelection === 'SCISSORS') {
computerScore +=2;
return playerSelection + ' loses vs ' + computerSelection + '. Try again!';
} else if( playerSelection === 'SCISSORS' && computerSelection === 'SCISSORS') {
return playerSelection + ' ties with ' + computerSelection + '. Try again!';
} else if( playerSelection === 'SCISSORS' && computerSelection === 'PAPER') {
humanScore += 1;
return playerSelection + ' wins vs ' +computerSelection + '. Congratulations!';
} else if( playerSelection === 'SCISSORS' && computerSelection === 'ROCK') {
computerScore +=2;
return playerSelection + ' loses vs ' + computerSelection + '. Try again!';
} else return 'Enter a valid move! Refresh the damn page!';
win_condition();
}
function win_condition() {
if( humanScore === 5 ) {
return 'Player Wins!';
}
if( computerScore === 5 ) {
return 'Computer wins!';
}
}
The program executes through one time and works, but doesn't repeat like I expect it to.
There are few issues in your code as highlighted in comment section:
Functions win_condition and game are never called.
Functions win_condition and playGround returns processed value but are accepted back.
if( humanScore === 5 ) or if( computerScore === 5 ) are always false. Condition to process is humanScore <= 5 || computerScore <= 5
if(humanScore <= 5 || computerScore <= 5) This will fail even if a player has more than 5 score.
let winner = 0;
let humanScore = 0;
let computerScore = 0;
function initializeGame() {
let playerSelection = prompt(' Enter Rock, Paper, or Scissors');
let computerSelection = computerPlay();
let msg = playRound(playerSelection, computerSelection);
console.log(msg);
if (humanScore <= 5 && computerScore <= 5) {
console.log('Current score: Human: ', humanScore, ' Computer: ', computerScore)
setTimeout(initializeGame, 0);
} else {
console.log(win_condition());
}
}
function computerPlay() {
let number = Math.floor(Math.random() * (3 + 1));
if (number == 1)
return 'Rock';
else if (number == 2)
return 'Paper';
else return 'Scissors';
}
function playRound(playerSelection, computerSelection) {
playerSelection = playerSelection.toUpperCase();
computerSelection = computerSelection.toUpperCase();
if (playerSelection === 'ROCK' && computerSelection === 'SCISSORS') {
humanScore += 1;
return playerSelection + ' wins vs ' + computerSelection + '. Congratulations!';
} else if (playerSelection === 'ROCK' && computerSelection === 'PAPER') {
computerScore += 2;
return playerSelection + ' loses vs ' + computerSelection + '. Try again!';
} else if (playerSelection === 'ROCK' && computerSelection === 'ROCK') {
return playerSelection + ' ties with ' + computerSelection + '. Try again!';
} else if (playerSelection === 'PAPER' && computerSelection === 'ROCK') {
humanScore += 1;
return playerSelection + ' wins vs ' + computerSelection + '. Congratulations!';
} else if (playerSelection === 'PAPER' && computerSelection === 'PAPER') {
return playerSelection + ' ties with ' + computerSelection + '. Try again!';
} else if (playerSelection === 'PAPER' && computerSelection === 'SCISSORS') {
computerScore += 2;
return playerSelection + ' loses vs ' + computerSelection + '. Try again!';
} else if (playerSelection === 'SCISSORS' && computerSelection === 'SCISSORS') {
return playerSelection + ' ties with ' + computerSelection + '. Try again!';
} else if (playerSelection === 'SCISSORS' && computerSelection === 'PAPER') {
humanScore += 1;
return playerSelection + ' wins vs ' + computerSelection + '. Congratulations!';
} else if (playerSelection === 'SCISSORS' && computerSelection === 'ROCK') {
computerScore += 2;
return playerSelection + ' loses vs ' + computerSelection + '. Try again!';
} else return 'Enter a valid move! Refresh the damn page!';
}
function win_condition() {
if (humanScore > 5) {
return 'Player Wins!';
}
if (computerScore > 5) {
return 'Computer wins!';
}
}
initializeGame();
I tried fixing your code as below and it works fine now.
Game method is called initially.
Game rounds are controlled / iterated through game method.
Condition for next round is altered to while( humanScore <= 5 && computerScore <= 5) {
Alerts are added to show messages after each round and final winner.
let winner = 0;
let humanScore = 0;
let computerScore = 0;
function computerPlay() {
let number = Math.floor(Math.random() * (3 + 1));
if (number == 1)
return 'Rock';
else if (number == 2)
return 'Paper';
else return 'Scissors';
}
game();
function game() {
while (humanScore <= 5 && computerScore <= 5) {
let playerSelection = prompt(' Enter Rock, Paper, or Scissors');
let computerSelection = computerPlay();
playerSelection = playerSelection.toUpperCase();
computerSelection = computerSelection.toUpperCase();
alert(playRound(playerSelection, computerSelection));
}
alert(win_condition());
}
function playRound(playerSelection, computerSelection) {
if (playerSelection == 'ROCK' && computerSelection == 'SCISSORS') {
humanScore += 1;
return playerSelection + ' wins vs ' + computerSelection + '. Congratulations!';
} else if (playerSelection == 'ROCK' && computerSelection == 'PAPER') {
computerScore += 2;
return playerSelection + ' loses vs ' + computerSelection + '. Try again!';
} else if (playerSelection == 'ROCK' && computerSelection == 'ROCK') {
return playerSelection + ' ties with ' + computerSelection + '. Try again!';
} else if (playerSelection == 'PAPER' && computerSelection == 'ROCK') {
humanScore += 1;
return playerSelection + ' wins vs ' + computerSelection + '. Congratulations!';
} else if (playerSelection == 'PAPER' && computerSelection == 'PAPER') {
return playerSelection + ' ties with ' + computerSelection + '. Try again!';
} else if (playerSelection == 'PAPER' && computerSelection == 'SCISSORS') {
computerScore += 2;
return playerSelection + ' loses vs ' + computerSelection + '. Try again!';
} else if (playerSelection == 'SCISSORS' && computerSelection == 'SCISSORS') {
return playerSelection + ' ties with ' + computerSelection + '. Try again!';
} else if (playerSelection == 'SCISSORS' && computerSelection == 'PAPER') {
humanScore += 1;
return playerSelection + ' wins vs ' + computerSelection + '. Congratulations!';
} else if (playerSelection == 'SCISSORS' && computerSelection == 'ROCK') {
computerScore += 2;
return playerSelection + ' loses vs ' + computerSelection + '. Try again!';
} else return 'Enter a valid move! Refresh the damn page!';
}
function win_condition() {
if (humanScore == 5) {
return 'Player Wins!';
}
if (computerScore == 5) {
return 'Computer wins!';
}
}

Why is my Rock Paper Scissors game not working? It keeps returning 'You chose Rock, you win', how do i fix this?

I have been stuck on this for so long, basically its a human vs computer, I type in say Rock and it gives me you chose rock you win, even when i chose the others it still returns that, can someone help me to figure out why it keeps doing this?
let person = prompt ("Rock, Paper, Scissors");
// Computer makes a choice
function computerPlay () {
let compchoice = ['Rock', 'Paper', 'Scissors'];
return compchoice[Math.floor(Math.random() *
compchoice.length)];
}
//Player vs Computer
function playRound (playerSelection, computerSelection) {
if (playerSelection === 'Rock' || computerSelection ===
'Scissors') {
return 'You chose ' + playerSelection + ',' + ' You win!';
} else if (playerSelection === 'Paper' || computerSelection ===
'Rock')
{
return 'You chose ' + playerSelection + ',' + ' You win!';
} else if (playerSelection === 'Scissors' || computerSelection ===
'Paper')
{
return 'You chose ' + playerSelection + ',' + ' You win!';
} else if (computerSelection === 'Rock' || playerSelection ===
'Scissors')
{
return 'Computer chose ' + computerSelection + ',' + 'Computer
wins!';
} else if (computerSelection === 'Paper' || playerSelection ===
'Rock')
{
return 'Computer chose ' + computerSelection + ',' + 'Computer
wins!';
} else if (computerSelection === 'Scissors' || playerSelection ===
'Paper')
{
return 'Computer chose ' + computerSelection + ',' + 'Computer
wins!';
} else if (computerSelection === playerSelection) {
return 'Its a draw!';
}else {
return 'Please chose Rock, Paper, or Scissors';
}
}
const playerSelection = 'rock';
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
It should just play a regular game of rock paper scissors, if i chose rock and the computer chose paper the computer should win. For now i am trying to make it play just one round.
You're using or statements. You need to use and statements. Take for example your first line:
if (playerSelection === 'Rock' || computerSelection === 'Scissors') {
return 'You chose ' + playerSelection + ',' + ' You win!';
}
It's saying:
if playerSelection equals Rock OR computerSelection equals Scissors return
So if playerSelection is Rock it'll return right there. What you need to use is an AND statement instead. Try this:
let playerSelection = prompt("Rock, Paper, Scissors");
// Computer makes a choice
function computerPlay() {
let compchoice = ['Rock', 'Paper', 'Scissors'];
return compchoice[Math.floor(Math.random() * compchoice.length)];
}
//Player vs Computer
function playRound(playerSelection, computerSelection) {
if (playerSelection === 'Rock' && computerSelection === 'Scissors') {
return 'You chose ' + playerSelection + ',' + ' You win!';
} else if (playerSelection === 'Paper' && computerSelection === 'Rock') {
return 'You chose ' + playerSelection + ',' + ' You win!';
} else if (playerSelection === 'Scissors' && computerSelection === 'Paper') {
return 'You chose ' + playerSelection + ',' + ' You win!';
} else if (computerSelection === 'Rock' && playerSelection === 'Scissors') {
return 'Computer chose ' + computerSelection + ',' + 'Computer wins!';
} else if (computerSelection === 'Paper' && playerSelection === 'Rock') {
return 'Computer chose ' + computerSelection + ',' + 'Computer wins!';
} else if (computerSelection === 'Scissors' && playerSelection === 'Paper') {
return 'Computer chose ' + computerSelection + ',' + 'Computer wins!';
} else if (computerSelection === playerSelection) {
return 'Its a draw!';
} else {
return 'Please chose Rock, Paper, or Scissors';
}
}
//const playerSelection = 'Rock';
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
Also there was a typo in your player selection on the third last line. The playerSelection string should be capitalized like the strings it's comparing too.

Categories

Resources