My Console will only return two inputs or just one - javascript

This is my first time posting, I am having issues getting all selections to generate on the console. If I capitalise rock at the bottom it will only show the "Scissor" result. If I do not capitalise it, it will only show the other two results, but not scissors. Any guidance would be helpful.
function computerPlay() {
let selection = ["Rock", "Paper", "Scissors"];
let computerSelection = selection[Math.floor(Math.random() * selection.length)];
return computerSelection;
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === "Rock") {
if (computerSelection === "Scissors")
return "You win mate, cheers!";
} else if (computerSelection === "Paper") {
return "You lost lad";
} else {
return "Draw";
}
}
const playerSelection = "rock"
const computerSelection = computerPlay()
console.log(playRound(playerSelection, computerSelection))

The problem is that if the player selection matches "Rock", you ignore the else if and else blocks and go to see if the computer selected Scissors. If that is the case you return "You win mate, cheers!", otherwise you don't return anything, that's why you are getting undefined. If the player does not play Rock then you go into either else if or else statement but the player never wins.
Here's what your playRound function might look like to work properly:
function playRound(player, comp) {
const selection = ["Rock", "Paper", "Scissors"];
pl = selection.indexOf(player);
co = selection.indexOf(comp);
if (co === pl) {
return 'Draw';
} else if (co - pl == 1) {
return 'You lost lad'
} else {
return 'You win mate, cheers!'
}
}
Here's a jsfiddle with a working code sample: https://jsfiddle.net/qjp1y1mg/

This code works fine for me. Try this. It is your code, but something was wrong (a bracket in a wrong place) and I have changed it.
function playRound(playerSelection, computerSelection) {
if (playerSelection === "Rock"){
if (computerSelection === "Scissors")
return "You win mate, cheers!";
else if (computerSelection === "Paper"){
return "You lost lad";
}
else {return "Draw";}
}
}
In your code this bracket was wrong. Because you are excluding other occurrences
if (playerSelection === "Rock") {
if (computerSelection === "Scissors")
return "You win mate, cheers!";
**}**

You're missing a curly bracket after the first inner if statement. The indentation of the code should be a sign that it is not nested the way you want it.
function playRound(playerSelection, computerSelection) {
if (playerSelection === "Rock") {
if (computerSelection === "Scissors"{
return "You win mate, cheers!";
} else if (computerSelection === "Paper") {
return "You lost lad";
} else {
return "Draw";
}
}
}
Here it is fixed. In future scan through your code and ask yourself which opening brackets correspond to whch closing brackets.

Related

Making "Rounds" in a Rock-Paper-Scissors Game?

I know this is a very popular/simple game to make, but I'm having a bit of trouble. I have made a rock-paper-scissors game in javascript and I've managed to make it work so that it will prompt the player to choose rock, paper, or scissors, get the computer to randomly pick an option, and a means for it to say "you lose" or "you win". The problem is that I'm required to make 5 rounds. That's the issue I'm having. I, for some reason, cannot seem to get the code to prompt the user 5 times. It only prompts the user once and runs the same code each time. My code is as follows:
function playRound(playerSelection) {
let computerSelection = getComputerSelection();
if (playerSelection == "rock") {
addToList(playerSelection, computerSelection);
if (computerSelection == "rock") {
result = "It's a tie";
}
else if (computerSelection == "paper") {
result = "You lost! Paper beats Rock";
computerScore++;
}
else {
result = "You won! Rock beats Scissors";
playerScore++;
}
}
else if (playerSelection == "paper") {
addToList(playerSelection, computerSelection);
if (computerSelection == "rock") {
result = "You won! Paper beats Rock";
playerScore++;
}
else if (computerSelection == "paper") {
result = "It's a tie";
}
else {
result = "You lost! Scissors beats Paper";
computerScore++;
}
}
else {
addToList(playerSelection, computerSelection);
if (computerSelection == "rock") {
result = "You lost! Rock beats Scissors";
computerScore++;
}
else if (computerSelection == "paper") {
result = "You won! Scissors beats Rock";
playerScore++;
}
else
result = "It's a tie";
}
h1.textContent = result;
player.textContent = playerScore;
computer.textContent = computerScore;
game();
}
function game() {
if (playerScore === 5) {
end.style.display = "block";
message.textContent = "You Won!";
}
else if (computerScore === 5) {
end.style.display = "block";
message.textContent = "You Lost!";
}
}
I would appreciate any help or direction that is given. I have tried putting my "playRound" function outside of the loop, but it gives me the same issue. What would make it prompt twice?
Thank you!
That's because you are asking for player's prompt only once i.e. it is outside the playRound method.
Try moving the following snippet into playround method.
const computerSelection = computerPlay();
const playerSelection = window.prompt("Enter your choice: ").toLowerCase();
I'm no expert in JS, so please correct me if i'm wrong, but what seems to be the case is that this variable:
const playerSelection = window.prompt("Enter your choice: ").toLowerCase();
is only initialized a single time with that function's return. (because it is outside any function) Accessing the variable only recalls it's value it was initialized with, causing the main loop to run over and over again with only that initial value.
replacing that line with someing like:
function getPlayerSelection(){
return window.prompt("Enter your choice:").toLowerCase()
}
and replacing everywhere you use playerSelection with getPlayerSelection() should fix this issue.

What is wrong with my if/else statements?

My first time and first project: Rock, Paper, Scissors. I have spent hours trying to figure out what could be wrong with my if/else statements. Regardless of conditions evaluated, it returns the first statement(It's a tie"). My code is as below;
const computerSelection = computerPlay();
const playerSelection = humanPlay();
function computerPlay() {
let gameItems = ["rock", "paper", "scissors"];
let randItem = gameItems[Math.floor(Math.random() * gameItems.length)];
return randItem.toLowerCase();
}
function humanPlay() {
let selectItem = prompt(
"Please pick one battle item: rock, paper, or scissors"
);
return selectItem.toLowerCase();
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return "It's a tie!";
} else if (computerSelection === "rock" && playerSelection === "scissors") {
return "You lose!";
} else if (computerSelection === "scissors" && playerSelection === "rock") {
return "You win";
} else if (computerSelection === "scissors" && playerSelection === "paper") {
return "You lose";
} else if (computerSelection === "paper" && playerSelection === "scissors") {
return "You win";
} else if (computerSelection === "paper" && playerSelection === "rock") {
return "You lose";
} else if (computerSelection === "rock" && playerSelection === "paper") {
return "You win";
} else {
return "Please play again";
}
}
console.log(computerSelection);
console.log(playerSelection);
console.log(playRound());
Add console.log(playerSelection, computerSelection) to the first line of the playRound() function. You'll see that both values are undefined, because you aren't passing those values into the function when you call it.
To fix, change the very last line to: console.log(playRound(computerSelection, playerSelection));

Rock-Paper-Scissors: the score is not recorded

I have created the function for the computer's choices and for the player's possible choices. However, for hours I've been trying to solve the issue of scorekeeping as it does not seem to work. I have set the requirements and I set global functions playerScore and computerScore = 0 and yet every time I call the gamescore function, the message which appears is "the tie" since for some reason the values of the two scores are always considered to be 0.
Any idea why might this be happening? I have done an extensive search online but in vain. I am still a beginner so on purpose, I use console.log and not innerHTML or any html/css style/button. The computerPlay() function is console-logged and every time go-live is refreshed, it gives a random choice so it works. For the second function, I do not know loops yet so I called it 5 times via console log to see the results. I also put specific playerSelection arguments so as to make the strings "You won, You lost, Draw again" cause if I did not, in all 5 times, the return was undefined and not one of these three strings/returns. If you refresh go live you will see the choices are changing randomly but the final sentence that should be given depending on the score after 5 rounds is always the same.
function computerPlay() {
let gameWords = ["Rock", "Paper", "Scissors"];
let randomWord = gameWords[Math.floor(Math.random() * gameWords.length)];
return randomWord;
};
console.log(computerPlay());
let playerScore = 0
let computerScore = 0
let draws = 0
function playRound(playerSelection, computerSelection) {
playerSelection = playerSelection.toLowerCase();
if (computerSelection === "Rock") {
if (playerSelection === "rock") {
return "Draw again."
draws++
} else if (playerSelection === "paper") {
return "You won!"
playerScore++;
} else if (playerSelection === "scissors") {
return "You lost!"
computerScore++
};
};
if (computerSelection === "Paper") {
if (playerSelection === "paper") {
return "Draw again."
draws++
} else if (playerSelection === "rock") {
return "You lost..."
computerScore++
} else if (playerSelection === "scissors") {
return "You won!"
playerScore++
};
};
if (computerSelection === "Scissors") {
if (playerSelection === "scissors") {
return "Draw again."
draws++
} else if (playerSelection === "paper") {
return "You lost..."
computerScore++
} else if (playerSelection === "rock") {
return "You won"
playerScore++
};
};
console.log(playerScore);
};
const computerSelection = computerPlay();
console.log(playRound("RoCk", computerSelection));
console.log(playRound("Scissors", computerSelection));
console.log(playRound("PAper", computerSelection));
console.log(playRound("paper", computerSelection));
console.log(playRound("PAPER", computerSelection));
function gameScore() {
if (playerScore > computerScore) {
return "You won the game!";
} else if (playerScore < computerScore) {
return "You lost, try again.";
} else if (playerScore === computerScore) {
return "It is a tie.";
}
}
console.log(gameScore());
You code returns and then increase counters:
return "Draw again."
draws++
as you return counter increase is not called.

My JavaScript function is not working - Trying to compare two functions and return a statement within the 2nd function

Hey StackOverflow community!
I'm new here and just started learning JS via Odin Project. I'm on my first JS project (rock, paper, scissors app) and am stuck....
I'll summarize what my intentions are below followed by my code.
//The user would input a value of either rock, paper, or scissors
//With the user value, it would be compared to the value of the computer's selection at random
//Depending on that comparison from both the user's input and the computer generated one, a return statement would be given
Have tried almost everything and not sure how to move forward! Please help.
function computerPlay() {
let gameOptions = ['rock', 'paper', 'scissors'];
const gameChoice = Math.floor(Math.random() * gameOptions.length);
console.log(gameChoice, gameOptions[gameChoice]);
}
function playRound(playerSelection,computerSelection){
if(playerSelection === 'rock' && computerSelection === 'paper') {
return console.log('You lose! Paper beats rock.');
}else if(playerSelection === 'paper' && computerSelection === 'scissors') {
return console.log('You lose! Scissors beats paper.');
}else if(playerSelection === 'scissors' && computerSelection === 'rock') {
return console.log('You lose! Rock beats scissors');
}else if(playerSelection === 'rock' && computerSelection === 'rock') {
return console.log('Its a draw!');
}else if(playerSelection === 'paper' && computerSelection === 'paper') {
return console.log('Its a draw!');
}else if(playerSelection === 'scissors' && computerSelection === 'scissors') {
return console.log('Its a draw!');
}else if(playerSelection === 'paper' && computerSelection === 'rock') {
return console.log('You win!')
}else if(playerSelection === 'rock' && computerSelection === 'scissors') {
return console.log('You win!')
}else {
return console.log('You win!')
}
}
const playerSelection = prompt('Choose either rock, paper, or scissors', '');
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
I was hoping the conditional statement within the 2nd function would be able to accomplish this. Still trying to wrap my head around functions, parameters, and arguments.
Any and all help would be GREATLY appreciated!! Thanks.
computerPlay() does not return any value. So computerSelection will be undefined.
Also, change this line console.log(playRound(playerSelection, computerSelection)); to just playRound(playerSelection, computerSelection); and remove the return statements from playRound().
Also, your logic can be greatly simplified. For example, if (playerSelection === computerSelection) console.log("It's a draw");
computerPlay function does not return any value. I think you should console each value before do playRound

Missing piece? Own rock paper scissor game

Not printing the winner in Rock,Paper,Scissor game. For some reason I cannot get my function DeclareWinner to return any of the strings I have created.
After I did a short test where I put a string outside of my if/else statements I managed to get a print. It seems that my returns are stuck in the local scope and does therefore not get printed.
var weapon = "rock"
function Userchoice(weapon){
if (weapon === "rock" || weapon === "paper" || weapon === "scissor") {
return weapon
}
else {
return "Invalid Choice";
}
}
function Computerchoice(){
var Number = Math.floor(Math.random() * 3);
if (Number === 0) {
return "rock";
}
else if (Number === 1) {
return "paper";
}
else if (Number === 2) {
return "scissor";
}
}
function DeclareWinner(Userchoice, Computerchoice){
if (Userchoice === Computerchoice){
return "Tiebreak";
}
else if (Userchoice === "rock" && Computerchoice === "scissor"){
return "User wins";
}
else if (Userchoice === "rock" && Computerchoice === "paper"){
return "Computer wins";
}
else if (Userchoice === "paper" && Computerchoice === "rock"){
return "User wins";
}
else if (Userchoice === "paper" && Computerchoice === "scissor"){
return "Computer wins";
}
else if (Userchoice === "scissor" && Computerchoice === "rock"){
return "Computer wins";
}
else if (Userchoice === "scissor" && Computerchoice === "paper"){
return "User wins";
}
}
console.log(Userchoice(weapon));
console.log(Computerchoice());
console.log(DeclareWinner(Userchoice, Computerchoice));
The expectation of console.log(DeclareWinner(Userchoice, Computerchoice)); is to print who is the winner.
(ps. I know there are several other rock paper scissor games who are more technical, I just wanted to try and create my own without specific guidance)
Thanks in advance.
You are not passing the correct arguments to your function. Userchoice and Computerchoice are both references to the corresponding functions and do not represent the (string) results of your previous function calls as you might expect.
You can easily verify this by adding a console.log(Userchoice), which prints:
function Userchoice(weapon){if(weapon==="rock"||weapon==="paper"||weapon==="scissor"){return weapon;}else{return"Invalid Choice";}}
You want to store the results of the function calls before printing and pass them to DeclareWinner.
Relevant part:
// Your unchanged code
// ...
let userchoice = Userchoice(weapon);
let computerchoice = Computerchoice();
console.log(userchoice);
console.log(computerchoice);
console.log(DeclareWinner(userchoice, computerchoice));
As a side note: Variable and method names usually start with a lower case letter.

Categories