Why is this console.log() failing? - javascript

I am creating Rock Paper Scissors for a class, and I'm trying to get it to log the winner in the console, but nothing happens when the button is pressed. I'm not sure why. Is there anything obviously wrong with my code here?
let playGame = function() {
let userChoice = prompt("Do you choose rock, paper or scissors?");
let computerChoice = Math.random();
let userWins = 0;
let compWins = 0;
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67 && computerChoice >= 0.34) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
let compare = function(choice1, choice2) {
if (choice1 === choice2) {
console.log("The result is a tie!");
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
console.log("User wins!");
} else {
console.log("Computer wins!");
}
} else if (choice1 === "paper") {
if (choice2 === "rock") {
console.log("User wins!");
} else {
console.log("Computer wins!");
}
} else if (choice1 === "scissors") {
if (choice2 === "paper") {
console.log("User wins!");
} else {
console.log("Computer wins!");
}
}
};
compare(userChoice, computerChoice);
}
<button class="playGame" onclick="playGame()">Play!</button>

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.

What am I doing wrong here? I get the prompt but no alert comes up after I make a selection

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.

JavaScript SyntaxError: Unexpected token else

I have this code for a rock, paper, scissors game that is basically homework. I have double-checked and it seems to be fine, however, when I run it it says:
SyntaxError: Unexpected token else,
any help will be very appreciated :) Please note that I am a newbie, so if the question is dumb, please be nice and help <3
I just edited the code a bit, since I had many "Overlook" mistakes. I also wanted to clarify that I need all the code located after the function statement to be inside the function, that is why I don't close the first { right away.
PD: Now I get: SyntaxError: Unexpected token =
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";
}
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 if (choice1 ==== "paper") {
if (choice2 === "rock") {
return "paper wins";
else if (choice2 === "scissors") {
return "scissors wins"; }
else {
return "Paper wins"; }
}
}
}
compare(userChoice, computerChoice)
Ok, to stay true to your homework, I kept the same format just fixed the issues.
here it is:
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";
}
console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
}
if (choice1 === "paper") {
if (choice2 === "rock") {
return "Paper wins!";
} else {
return "Paper looses!";
}
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "Rock wins!";
} else {
return "Rock looses!";
}
}
if (choice1 === "scissors") {
if (choice2 === "paper") {
return "Scissors wins!";
} else {
return "Scissors looses!";
}
}
}
compare(userChoice, computerChoice)
Consider re-writing it in a little easier way.
See fiddle https://jsfiddle.net/DIRTY_SMITH/c7ww2hmz/1/
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";
}
alert("the computer picked " + computerChoice);
if ((computerChoice === "rock" && userChoice === "papper") || (computerChoice === "papper" && userChoice === "scissors") || (computerChoice === "scissors" && userChoice === "rock")) {
alert("you won");
} else if (computerChoice == userChoice) {
alert("It's a tie");
} else {
alert("you loose");
}
You will find your debugging much easier if you
properly indent,
use braces for any but the simplest if statements
For example:
if (choice1 == choice2) return "tie"; /* simple 1-line if is ok */
if (choice1 == "rock") {
if (choice2 == "scissors") { /* more complex, always use braces */
return "rock wins"; /* always indent nicely */
} else {
return "paper wins";
}
}
/* ... and so on ... */
Always properly format your code. You are missing a bunch of } before the else statements. Always use semi-colons at the end of a line (no, you don't technically need to be it is extremely good practice).
Also, you need to watch your equals. You had one ==== instead of ===
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";
}
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 if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else if {
return "paper wins";
} else {
return "Paper wins";
}
}
}
}
compare(userChoice, computerChoice)

I'm learning Javascript, I'm trying to find out why my "Rock paper scissors" game wont work . .

Ok, so I'm sure this code is FILLED with inaccuracies, but I got really confused when I finished this lesson on the CodeAcademy website and then tried to copy/paste it into my own javascript.html file (where I keep stuff I'm learning) and the code wouldn't work! Then I plugged it in to jsfiddle to play with it and still couldn't get it to work.
here is the original code that I copy/pasted from CodeAcademy:
http://jsfiddle.net/p46pnwvx/
Javascript:
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";
}
console.log("Player: " + userChoice);
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 win!";
}
}
else if (choise1 === "scissors") {
if (choice2 === "paper") {
return "Scissors win!";
}
else {
return "Rock wins!";
}
}
}
console.log(compare(userChoice, computerChoice));
When this loads, the PROMPT box comes up, but nothing else happens.
Here is what I was playing around with, trying to make the game start and reset with a button, which I thought would be more useful (and educational) than having a game just up and start on it's own.
http://jsfiddle.net/o9ckcy52/
HTML
<h1>Rock, Paper, Scissors Game!</h1>
<button id="start" type="button" onclick="startGame()">Wanna Play?</button>
<p id="choice"></p>
<p id="result"></p>
Javascript
function startGame(){
document.getElementById("start").innerHTML = "Play again?"; // reset's button
return prompt("Do you choose rock, paper or scissors?");
var userChoice = startGame(); // stores result of startGame as user's choice
var computerChoice = Math.random(); // randomly creates computer's choice
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
function compare(choice1, choice2) { //compares user's choice and computer's choice
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 win!";
}
}
else if (choise1 === "scissors") {
if (choice2 === "paper") {
return "Scissors win!";
}
else {
return "Rock wins!";
}
}
}
document.getElementById("choice").innerHTML= // prints user and computer selections
"Player: " + userChoice + /r
"Computer: " + computerChoice;
document.getElementById("result").innerHTML = // prints result of selection comparisons
(compare(userChoice, computerChoice));
}
Here the html works, but nothing happens when you click the button.
Any help given would be appreciated.
I updated your code. There were just some minor issues that were fixed. For example, the userChoice should be assigned to the prompt. Also, your regex /r is invalid and should also be wrapped in quotes, which becomes "\r". You also mispelled one of your choice1's.
Run the snippet below:
function startGame() {
document.getElementById("start").innerHTML = "Play again?"; // reset's button
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random(); // randomly creates computer's choice
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
function compare(choice1, choice2) { //compares user's choice and computer's choice
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 win!";
}
} else if (choice1 === "scissors") {
if (choice2 === "paper") {
return "Scissors win!";
} else {
return "Rock wins!";
}
}
}
document.getElementById("choice").innerHTML = // prints user and computer selections
"<pre>" +
"Player: " + userChoice + "\r" +
"Computer: " + computerChoice +
"</pre>";
document.getElementById("result").innerHTML = // prints result of selection comparisons
(compare(userChoice, computerChoice));
}
document.getElementById("start").onclick = startGame;
<h1>Rock, Paper, Scissors Game!</h1>
<button id="start">Wanna Play?</button>
<p id="choice"></p>
<p id="result"></p>
1- Use http://jsbeautifier.org/ to clean up your js code, you are mixing Spaces and Tabs. Also it will clean your code alignment.
2- return prompt("Do you choose rock, paper or scissors?");
will always return before any code get's executed.
3- You are calling startGame() from the button click right? then why you are calling it again from inside the function it self?
Which indicates that you have a missing closing bracket } for the function.
var userChoice = startGame();

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

Categories

Resources