I have this code right now for my Rock Paper Scissors JS game. In my compare function, I tried to program it so that it will display an alert message if either "Rock", "Paper", or "Scissors" is not entered. It does not work though, and I get no response when I enter in a different string than the 3 choices that work.
var userChoice = prompt("Rock, Paper, or Scissors");
var computerChoice = Math.random();
var compchoice = function ()
{
if (computerChoice <= 0.34)
{
return computerChoice = "Rock";
}
else if(computerChoice <= 0.67 && computerChoice >= 0.35)
{
return computerChoice = "Paper";
}
if (computerChoice >= 0.68)
{
return computerChoice = "Scissors";
}
};
var compare = function (choice1, choice2)
{
if (computerChoice === "Rock" || "Paper" || "Scissors")
{
if (choice1 === choice2)
{
return alert("The result is a tie!");
}
else if (choice1 === "Rock")
{
if (choice2 === "Scissors")
{
return alert("Rock crushes Scissors!");
}
else if (choice2 === "Paper")
{
return alert("Paper covers Rock!");
}
}
if (choice1 === "Scissors")
{
if (choice2 === "Rock")
{
return alert("Rock crushes Scissors!");
}
else if (choice2 === "Paper")
{
return alert("Scissors cuts Paper!");
}
}
else if (choice1 === "Paper")
{
if (choice2 === "Rock")
{
return alert("Paper covers Rock!");
}
else if (choice2 === "Scissors")
{
return alert("Scissors cuts Paper!");
}
}
}
else
{
return alert("Please type Rock, Paper, or Scissors next time");
}
};
compchoice();
compare(userChoice, computerChoice);
Any reasons why?
computerChoice === "Rock" || "Paper" || "Scissors" is always true, since it parses as:
(computerChoice === "Rock") || ("Paper") || ("Scissors")
And "Paper" is a truthy value.
Also, you seem to be comparing computerChoice, not userChoice.
Fixed:
if (userChoice === "Rock" || userChoice === "Paper" || userChoice === "Scissors")
Or:
// array and indexOf
if (["Rock", "Paper", "Scissors"].indexOf(userChoice) > -1)
// doesn't work in IE8
Or:
// regex
if (/^(Rock|Paper|Scissors)$/.test(userChoice))
if (computerChoice === "Rock" || computerChoice === "Paper" || computerChoice === "Scissors")
TADA!.... "Paper" as a string resolves as true :D
You have a logic error in your first if in the compare:
if (computerChoice === "Rock" || "Paper" || "Scissors")
Should be:
if (computerChoice === "Rock" || computerChoice === "Paper" || computerChoice === "Scissors")
Edit: why you have this if statement in the first place does not make sense to me. You want to compare choice1 against choice2.
You can't do comparisons like this:
if (computerChoice === "Rock" || "Paper" || "Scissors")
You need to check each separately, like:
if(computerChoice === "Rock" || computerChoice === "Paper" || computerChoice === "Scissors")
The answer is given already, but you could optimize your code even more. This is what I would write:
var options=["Rock","Paper","Scissors"];
var beats=["crushes","covers","cuts"];
function play(choice){// "choice" = index of option
if(choice<0||choice>options.length-1){alert("invalid input");}
var loser=choice>0?choice-1:options.length-1; // what would lose from "choice"
var winner=choice<options.length-1?choice+1:0; // what would beat "choice"
switch(Math.floor(Math.random()*3)){ //chance of 1 in 3
case 0: alert(options[choice]+beats[choice]+options[loser]); break;// win :-)
case 1: alert(options[winner]+beats[winner]+options[choice]); break;// lose :-(
case 2: alert("The result is a tie!"); break;
}
}
play(0);// 0=rock!
Since the chance is 1 in 3 you only have to create 3 static outcomes (win, lose or draw), and generate the message for it.
I think you need the comparison to be userChoice like
if (userChoice === "Rock" || "Paper" || "Scissors")
{
Related
I'm a complete beginner in Javascript and coding in general.
I'm trying to make a simple command-line javascript paper-rock-scissors game but I keep getting a bug. I've created a function for the computer to randomly pick paper, rock or scissors and getting the user's selection from a prompt. To get the result I've written an if else statement that will show an alert with the outcome but regardless the input it always shows a tie. It seems that the code always ends up on the else part of the statement and I can't figure out why. Can someone help me with what's wrong with the statement and why it skips to else?
function computerPlay() {
let computerChoices = ["paper", "rock", "scissors"];
let computerChoice = computerChoices[Math.floor(Math.random() * computerChoices.length)];
return computerChoice;
}
function playRound(computerSelection, playerSelection) {
if (computerSelection === "rock" && playerSelection.toLowerCase() === "paper") {
alert("You win, paper beats rock!")
} else if (computerSelection === "paper" && playerSelection.toLowerCase() === "scissors") {
alert("You win, scissors beat paper!")
} else if (computerSelection === "scissors" && playerSelection.toLowerCase() === "rock") {
alert("You win, rock beat scissors")
} else if (computerSelection === "rock" && playerSelection.toLowerCase() === "scissors") {
alert("You loose, rock beats scissors")
} else if (computerSelection === "paper" && playerSelection.toLowerCase() === "rock") {
alert("You loose, paper beats rock")
} else if (computerSelection === "scissors" && playerSelection.toLowerCase() === "paper") {
alert("You loose, scissors beats paper")
} else {
alert("even steven")
}
}
for (let i = 1; i <= 5; i++) {
computerPlay()
const playerSelection = prompt("Please enter your choice", " ");
const computerSelection = computerPlay();
console.log(computerSelection);
console.log(playerSelection);
playRound(computerSelection, playerSelection);
}
Remove " " from the prompt, it's adding an unnnecessary white space causing issues in your if statements
function computerPlay() {
let computerChoices = ["paper", "rock", "scissors"];
let computerChoice = computerChoices[Math.floor(Math.random() * computerChoices.length)];
return computerChoice;
}
function playRound(computerSelection, playerSelection) {
if (computerSelection === "rock" && playerSelection.toLowerCase() === "paper") {
alert("You win, paper beats rock!")
} else if (computerSelection === "paper" && playerSelection.toLowerCase() === "scissors") {
alert("You win, scissors beat paper!")
} else if (computerSelection === "scissors" && playerSelection.toLowerCase() === "rock") {
alert("You win, rock beat scissors")
} else if (computerSelection === "rock" && playerSelection.toLowerCase() === "scissors") {
alert("You loose, rock beats scissors")
} else if (computerSelection === "paper" && playerSelection.toLowerCase() === "rock") {
alert("You loose, paper beats rock")
} else if (computerSelection === "scissors" && playerSelection.toLowerCase() === "paper") {
alert("You loose, scissors beats paper")
} else {
alert("even steven")
}
}
for (let i = 1; i <= 5; i++) {
computerPlay()
const playerSelection = prompt("Please enter your choice");
const computerSelection = computerPlay();
console.log(computerSelection);
console.log(playerSelection);
playRound(computerSelection, playerSelection);
}
I just got into JavaScript and I wrote a simple rock paper scissors game, but I am unable to actually make it run.
var userChoice = prompt("Rock, Paper, or Scissors?")
var computerChoice = math.random()
if (computerChoice < 0.33) {
computerChoice = "Rock"
} else if (computerChoice < 0.66) {
computerChoice = "Paper"
} else {
computerChoice = "Scissors"
};
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return "Tie, no one wins!"
}
};
if (userChoice === "Rock") {
if (computerChoice === "Scissors") {
return "You win!"
} else if (computerChoice === "Paper") {
return "You lose!"
}
};
if (userChoice === "Paper") {
if (computerChoice === "Rock") {
return "You win!"
} else if (computerChoice === "Scissors") {
return "You Lose!"
}
};
if (userChoice === "Scissors") {
if (computerChoice === "Paper") {
return "You win!"
} else if (computerChoice === "Rock") {
return "You Lose!"
}
};
console.log("Your choice was: " + userChoice);
console.log("The computer chose: " + computerChoice);
compare(userchoice, computerChoice);
This is the code I wrote. I haven't been able to troubleshoot it to see if it works, but I need a place to run it. Please help!!
You can create an html document then embed your javascript code into it.
Learn more here: https://www.javatpoint.com/how-to-call-javascript-function-in-html#:~:text=To%20include%20our%20JavaScript%20file,file%20where%20it%20is%20stored.
I'm trying to build a rock, paper, scissors game in JS. I think I have all the logic done correctly, it must be a syntax error, but for the life of me I can't find it. Thanks.
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
var compare = function(choice1, choice2) {
if(choice1 === choice2) {
alert( "It's a tie.");
}
if (choice1 === "scissors") {
if (choice2 === "rock") {
alert("You lose")
}
if (choice2 === "paper") {
alert("you win")
}
}
if (choice1 === "paper") {
if (choice2 === "rock") {
alert("You Win.")
}
if (choice2 === "scissors") {
alert("You lose.")
}
}
if (choice1 ==="scissors") {
if (choice2 === "rock"){
alert("You lose.")
}
if (choice2 === "paper") {
alert("You win.")
}
}
}
compare(userChoice,computerChoice);
First of all Welcome to StackOverflow, now your question ...
to tell you plain-and-simple you kind'a forgot to test for rock ... look at your code, you do the scissors, the paper, then scissors again 🤔
and for future programing, don't hold a float and a string in the same variable, javascript is forgiven, but it's still bad coding habits 😊👌
it would be more evident if you refractored your code a bit :
var userChoice = prompt("Do you choose rock, paper or scissors?");
var rnd = Math.random(); // use a new variable to hold the float number
var computerChoice = rnd < 0.34
? "rock" : rnd <= 0.67
? "paper" : "scissors";
// just so the example does not use alert() function
var alert = function(msg) { console.log(msg); }
var compare = function(choice1, choice2) {
if (choice1 === choice2) alert( "It's a tie.");
if (choice1 === "scissors") {
if (choice2 === "rock") alert("You lose");
if (choice2 === "paper") alert("you win");
}
if (choice1 === "paper") {
if (choice2 === "rock") alert("You Win.");
if (choice2 === "scissors") alert("You lose.");
}
if (choice1 ==="rock") { // you have "scissors" again here
if (choice2 === "scissors") alert("You lose.")
if (choice2 === "paper") alert("You win.");
}
}
compare(userChoice,computerChoice);
The issue come from the last if block, bellow a snippet with the correction :
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
alert("It's a tie.");
}
if (choice1 === "scissors") {
if (choice2 === "rock") {
alert("You lose")
}
if (choice2 === "paper") {
alert("you win")
}
}
if (choice1 === "paper") {
if (choice2 === "rock") {
alert("You Win.")
}
if (choice2 === "scissors") {
alert("You lose.")
}
}
if (choice1 === "rock") {
if (choice2 === "paper") {
alert("You lose.")
}
if (choice2 === "scissors") {
alert("You win.")
}
}
}
compare(userChoice, computerChoice);
this is not meant to be an answer but as I look at your compare function it appears to me it can be greatly simplified. Consider the following:
let choice1 = user choice: 1,2 or 3 (rock,paper,scissor)
let choice2 = computer choice: 1,2 or 3 (roc, paper,scissor)
choice1 = choice1 % 3;
if (choice1 == choice2){
tie;
}else if(choice1 > choice2){
choice1;
}else
choice2;
}
My way...
const choices = ['rock','paper','scissors']
playBt.onclick=()=>
{
if (playBt.textContent==='play!')
{
let h = parseInt( humanChoice.value )
, c = Math.floor(Math.random() *3)
, win = (h+1) %3
;
result.textContent = ' Computer choise is : ' + choices[c]
+ ((c===h) ? " -> It's a tie."
: (c===win) ? " -> You lose." : " -> You win.")
playBt.textContent = 'clear'
}
else
{
result.textContent = '?'
playBt.textContent = 'play!'
}
}
<select id="humanChoice">
<option value="0" selected >rock</option>
<option value="1">paper</option>
<option value="2">scissors</option>
</select>
<button id="playBt">play!</button>
<p id="result">?</p>
I guess it may be a copy paste issue. The last if clause(line 31-38) should be for rock combination instead of scissors.
Suggested Corrected code:
if (choice1 ==="rock") {
if (choice2 === "scissors"){
alert("You win.")
}
if (choice2 === "paper") {
alert("You lose.")
}
}
Your code:
if (choice1 ==="scissors") {
if (choice2 === "rock"){
alert("You lose.")
}
if (choice2 === "paper") {
alert("You win.")
}
}
Hope this helps.
could someone give me hand please, its my 3rd day learning JS and I am trying to add do while loop until rock, paper or scissors is entered to the prompt but it seems like it doesnt work, trying to figure it out for few hours now... With this code prompt always appears doesnt matter what I enter.
// a. User makes a choice
var userChoice = prompt("Do you choose ROCK, PAPER or SCISSORS?");
do {
userChoice = prompt("Do you choose ROCK, PAPER or SCISSORS?");
}
while (userChoice != "rock" && userChoice != "paper" && "scissors");
var computerChoice = Math.random();
// b. Computer makes a choice
if (computerChoice <= 0.33) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
var choice1 = userChoice;
var choice2 = computerChoice;
// c. A compare function will determine who wins
function compare (choice1, choice2){
if (choice1 === choice2){
alert("A tie!");
}
else if (choice1 === "rock"){
if (choice2 === "paper"){
alert("Computer chose PAPER, you LOSE!");
} else
alert("Computer chose SCISSORS, you WIN!");
}
else if (choice1 === "paper"){
if (choice2 === "scissors"){
alert("Computer chose SCISSORS, you LOSE!");
} else
alert("Computer chose ROCK, you WIN!");
}
else if (choice1 === "scissors"){
if (choice2 === "rock"){
alert ("Computer chose ROCK, you lose!");
} else
alert("Computer chose PAPER, you WIN!");
}
}
compare (userChoice, computerChoice);
console.log("You chose:", choice1, "||", "Computer chose:", choice2);
There are couple of issues in your code:
You are missing the variable name userChoice in the and comparison within while condition
You do not require to prompt the user choice when the code run (in the first line) and inside the do block. You can simply prompt for user choice in do block.
var userChoice;
do {
userChoice = prompt("Do you choose ROCK, PAPER or SCISSORS?");
}
while (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors");
var computerChoice = Math.random();
// b. Computer makes a choice
if (computerChoice <= 0.33) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
var choice1 = userChoice;
var choice2 = computerChoice;
// c. A compare function will determine who wins
function compare (choice1, choice2){
if (choice1 === choice2){
alert("A tie!");
}
else if (choice1 === "rock"){
if (choice2 === "paper"){
alert("Computer chose PAPER, you LOSE!");
} else
alert("Computer chose SCISSORS, you WIN!");
}
else if (choice1 === "paper"){
if (choice2 === "scissors"){
alert("Computer chose SCISSORS, you LOSE!");
} else
alert("Computer chose ROCK, you WIN!");
}
else if (choice1 === "scissors"){
if (choice2 === "rock"){
alert ("Computer chose ROCK, you lose!");
} else
alert("Computer chose PAPER, you WIN!");
}
}
compare (userChoice, computerChoice);
console.log("You chose:", choice1, "||", "Computer chose:", choice2);
prompt always appears because
userChoice != "rock" && userChoice != "paper" && "scissors"
always evaluates to true. You probably wanted
userChoice != "rock" && userChoice != "paper" && userChoice != "scissors"
Homework help vampire alert. I'm trying to add a user input string validation function to the "Rock, paper, scissors" game on Codecademy. The text editor on the Codecademy site validates my code as correct, but I'm not getting the expected behavior, which is to return an alert if the user fails to enter either a "rock", "paper", or "scissors" string.
I've piped together the conditions in the if statement below:
var userChoice = prompt("Do you choose rock, paper or scissors?");
function validateUserChoice(userChoice) {
if (userChoice !== "rock" || userChoice !== "paper" || userChoice !== "scissors") {
alert("You can only select rock, paper or scissors!");
return false;
}
};
And here's the rest of the game. When the preceding function is invoked, it appears that the compare function below is bypassed and whatever string the user typed into the prompt is printed to the screen (see console.log("User Choice: " + userChoice); at the bottom). Otherwise, the game returns the expected behavior when the function above is commented out:
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
};
var compare = function(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return "The result is a tie!";
};
if (userChoice === "paper") {
if (computerChoice === "rock") {
return "paper wins";
} else {
if (computerChoice === "scissors") {
return "scissors wins";
}
}
};
if (userChoice === "scissors") {
if (computerChoice === "rock") {
return "rock wins";
} else {
if (computerChoice === "paper") {
return "scissors wins";
}
}
}
};
console.log("User Choice: " + userChoice);
console.log("Computer Choice: " + computerChoice);
compare(userChoice, computerChoice);
Try changing the condition like below,
function validateUserChoice(userChoice) {
if (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors") {
alert("You can only select rock, paper or scissors!");
return false;
}
};