JavaScript (ROCK, PAPER, SCISSORS) do while prompt - javascript

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"

Related

How do I run my Rock Paper Scissors Game?

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.

How to make a counter for rock, paper, scissors game?

I'm trying to make a rock, paper, scissors game for class, but I'm stuck with creating a counter. I declared global variables and thought it would be simple to just add +1 every time somebody wins, but it doesn't seem to be working. Here's my code of what I'm using now. What should I add to make the counter work?
var game = false;
var playerScore = 0;
var compScore = 0;
while (game != true){
var userChoice = prompt("Pick your poison: Rock, paper, or scissors. Type 'exit' to quit.").toLowerCase();
if (userChoice === "rock" || userChoice === "paper" || userChoice === "scissors"){
var computerChoice = randomizeComputer();
console.log("You chose: " + userChoice);
console.log("Computer chose: " + computerChoice);
compare(userChoice);
} else if (userChoice === "exit"){
console.log("Thanks for playing");
game = true;
} else {
console.log("Sorry, you typed something we couldn't recognize");
}
}
function compare (choice){
//If it's the same thing
if (choice === computerChoice){
return console.log("It's a tie");
}
//If user chooses rock
if (choice === "rock"){
if (computerChoice === "scissors"){
return console.log("You win!");
counter(1);
} else if (computerChoice === "paper"){
return console.log("You lose!");
}
}
//If user chooses paper
if (choice === "paper"){
if (computerChoice === "rock"){
return console.log("You win!");
} else if (computerChoice === "scissors"){
return console.log("You lose!");
}
}
//If user chooses scissors
if (choice === "scissors"){
if (computerChoice === "paper"){
return console.log("You win!");
} else if (computerChoice === "rock"){
return console.log("You lose!");
}
}
}
//Randomizes
function randomizeComputer () {
var i = Math.floor(Math.random() * 3 + 1);
if (i === 1){
return "rock";
} else if (i === 2){
return"paper";
} else {
return "scissors";
}
}

Validate user input string passed into Javascript prompt method

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;
}
};

Rock,Paper,Scissors Game on Codecademy

I tried to incorporate the additional suggestions given on the website(Codecademy), viz. the feature in which there's a tie and the provision for taking input again. This is the error-containing piece of code:
var userChoice, computerChoice;
var choicesDetermination = function(){
userChoice = prompt("Do you choose rock, paper or scissors?");
if(userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors"){
userChoice = prompt("Invalid input. Please try again.\n Do you choose rock, paper or scissors?");
}
computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log("User: " + userChoice);
console.log("Computer: " + computerChoice);
}
var compare = function(choice1, choice2){
if(choice1 === choice2){
console.log("The result is a tie! Let's try one more time.");
choicesDetermination();
compare(userChoice, computerChoice);
}
else if(choice1 === "rock"){
if(choice2 === "scissors"){
return "rock wins";
}
else{
return "paper wins";
}
}
else if(choice1 === "paper"){
if(choice2 === "rock"){
return "paper wins";
}
else{
return "scissors wins";
}
}
else{
if(choice1 === "scissors"){
if(choice2 === "paper"){
return "scissors wins";
}
else{
return "rocks wins";
}
}
}
}
choicesDetermination();
compare(userChoice, computerChoice);
Here, choicesDetermination() is the function in which I've taken the input and stored them in userChoice, computerChoice(both global variables).
I don't know why but the code seems to run fine when I ask for the input again; the variables are changed correctly. But the function compare() doesn't run correctly; the return statements don't get printed to screen.
What I've always found with Code Academy is that the output strings have to be annoyingly perfect, so check that first!
I think the issue with your code is that you're actually supposed to return "The result is a tie! Let's try one more time." not console.log.
Please find my complete, passing code below:
var userChoice = prompt("Do you choose rock, paper or scissors?");
if (userChoice != "rock" && "scissors" && "paper") {
alert("Please enter 'rock', 'scissors', or 'paper' as shown.");
userChoice = prompt("Type carefully, please: 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";
} console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
}
else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock wins";
}
else {
return "paper wins";
}
}
else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
}
else {
return "scissors wins";
}
}
else {
if (choice2 === "rock") {
return "rock wins";
}
else {
return "scissors wins";
}
}
};
compare(userChoice, computerChoice);

Rock paper scissors tie case on JavaScript

I am currently learning from CodeAcademy JS course and they told me to try to implement a retry
case if the choice of the user and the choice of the computer are the same.
Firstly, allow me to apologize for my bad indentation, as I am still learning how to properly ident my code.
Take a look at the if(choice1 === choice2).Under it I have written my way of performing a retry case, but it failed.I would be very glad to recieve help on how should I create a retry case if there is a tie between the choices?
Thanks in advance!
var userChoice = prompt("Do you choose rock, paper or scissors?");
while (userChoice !=="rock" && userChoice !=="paper" && userChoice !=="scissors"){
confirm(userChoice + " isn't rock, paper or scissors. How about you try again?");
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";
}
console.log("You Chose: "+userChoice);
console.log("The Computer Chose: "+computerChoice);
var compare = function (choice1,choice2){
if (choice1 === choice2) {
userChoice = prompt("Choose again");
computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
compare(userChoice,computerChoice);
}
if (choice1 === "rock"){
if (choice2 === "paper"){
return "Paper wins";
}
if (choice2 === "scissors") {
return "Rock wins";
}
}
if (choice1 === "paper"){
if (choice2 === "scissors"){
return "Scissors wins!";
}
if (choice2 === "rock"){
return "Paper wins!";
}
}
if (choice1 === "scissors"){
if (choice2 === "rock"){
return "Rock wins!";
}
if (choice2 === "paper"){
return "Scissors wins!";
}
}
};
compare(userChoice,computerChoice);
Here you go:
function rockpaperscissors() {
var userChoice = prompt("Do you choose rock, paper or scissors?");
while (userChoice !=="rock" && userChoice !=="paper" && userChoice !=="scissors"){
confirm(userChoice + " isn't rock, paper or scissors. How about you try again?");
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";
}
console.log("You Chose: "+userChoice);
console.log("The Computer Chose: "+computerChoice);
var choice1 = userChoice;
var choice2 = computerChoice;
if (choice1 === choice2){
confirm('Sorry, but there was a tie. You and the computer are equals. Let\'s try again anyways.');
return rockpaperscissors();
}
if (choice1 === "rock"){
if (choice2 === "paper"){
return "Paper wins";
}
if (choice2 === "scissors") {
return "Rock wins";
}
}
if (choice1 === "paper"){
if (choice2 === "scissors"){
return "Scissors wins!";
}
if (choice2 === "rock"){
return "Paper wins!";
}
}
if (choice1 === "scissors"){
if (choice2 === "rock"){
return "Rock wins!";
}
if (choice2 === "paper"){
return "Scissors wins!";
}
}
}
confirm(rockpaperscissors());
Now then, to explain what I did:
Everything now lives within the rockpaperscissors function. This way you can call it from within itself without duplicating code.
Reindented everything with two spaces. Seriously, indentation is important.
If there is a tie, it'll confirm you telling you that there's a tie, followed by restarting itself.
Once a not-tie has been reached, it will confirm you the result.
Additionally, because I felt like it, a very refined version that uses lots of complicated JS:
function rockpaperscissors() {
var user = '';
do {
user = prompt((user!==''?'Invalid answer. ':'')+'Choose rock, paper, or scissors.');
} while (['rock', 'paper', 'scissors'].indexOf(user) == -1);
var computer = ['rock', 'paper', 'scissors'][Math.floor(Math.random()*3)];
switch (user+'|'+computer) {
case "rock|scissors":
case "paper|rock":
case "scissors|paper":
return "User Wins!";
case "rock|rock":
case "paper|paper":
case "scissors|scissors":
return rockpaperscissors();
default:
return "Computer Wins!";
}
}
confirm(rockpaperscissors());
I'm not too clear on how your program is running but what you could do is break up you code into different functions. For example, have a function just to handle moves, another one that handles game logic (the score, looking for fouls, etc...)
If you had a function that would return the computers move, you would just use your same logic to say if (choice === computerMove) However, I would not compare computerMove with whatever the user types in with the identity comparison because you never know if the user will misspell rock or something. You could have this 'game logic' function return maybe a value that would tell if there is a winner, if both players made the same move (foul), or which turn it is.
If you're really hurting for a code snippet, let me know and I'll create one.

Categories

Resources