JavaScript SyntaxError: Unexpected token else - javascript

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)

Related

Why is this console.log() failing?

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>

Why is this javascript function call not working?

Calling my function yields a syntax error, everything else works on its own. The error is thrown on.
console.log(compare(userChoice, computerChoice));
The error is:
SyntaxError: Unexpected token {
Any ideas?
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)
{
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 (choice1 === "scissors")
{
if (choice2 === "paper"
{
return "paper wins"
}
else
{
return "scissors wins"
}
}
}
console.log(compare(userChoice, computerChoice));
Yes, you forgot the ) at if (choice2 === "paper".
As others have pointed out, one of your closing )s was missing.
With lots of nested if-else blocks, it's easy to lose track of your ()s and {}s. You can write much shorter, nicer, easier to read code if you utilise other conditional statements and expressions, namely the switch statement and the ? operator:
function compare(choice1, choice2) {
if (choice1 === choice2) {
return 'The result is a tie';
}
switch(choice1) {
case 'rock':
return choice2 === 'scissors' ? 'rock wins' : 'paper wins';
case 'paper':
return choice2 === 'rock' ? 'paper wins' : 'scissors win';
case 'scissors':
return choice2 === 'paper' ? 'scissors win' : 'rock wins';
}
}
As you can see, this code has much fewer ()s and {}s.

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

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