I've been stuck on 7/9 for 2 nights now. This is a rock, paper scissor game. I can't figure out what is wrong. I tried the online lint and it also says my line 22 is an error(Expected an identifier and instead saw 'else'). Following the instructions I wrote another else if under the existing code inside the compare function.
my code :
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{
return"paper wins";
}
else if(choice1 ==="paper");{
if(choice2 ==="rock")
return("paper wins");
}
else{
return"scissors wins";
}
}
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{
return"paper wins";
}
else if(choice1 ==="paper");{ -- on this there is semicolon after elseif block.. and how come else if is there after else block..
if(choice2 ==="rock")
return("paper wins");
}
else{
return"scissors wins";
}
}
I am assuming you are a beginner. Write a very clean code, take care of spaces and tabs, it is the best way to solve your debugging problems. there is indeed problem on line 22, you have put a semi-colon after a conditional statement.
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 {
return "paper wins";
} else if(choice1 === "paper"){//here the error was.
if(choice2 === "rock")
return("paper wins");
} else {
return "scissors wins";
}
}
else if(choice1 ==="paper");{
if(choice2 ==="rock")
return("paper wins");
}
you're terminating you're else if after the condition with ;
it should be:
else if(choice1 ==="paper"){
if(choice2 ==="rock")
return("paper wins");
}
I'm seeing several syntax errors in your code. It should look like the following:
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 {
return "paper wins";
}
} else if(choice1 ==="paper") {
if(choice2 ==="rock") {
return("paper wins");
} else {
return"scissors wins";
}
}
}
You cant have an else if after an else statement, thats what is causing your error message error(Expected an identifier and instead saw 'else')
And of course no semicolorn before the line really ends
https://jsfiddle.net/8hcpfnhw/
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"){ // I changed this to else if instead of else
return "paper wins";
}
else if(choice1 ==="paper") {
if(choice2 ==="rock")
return("paper wins");
}
else{ // only else as last check
return"scissors wins";
}
}
Related
I have created a basic rock paper scissors game and i cannot return the result inside the function.
I've been try everything
link console logging the compare and also putting it inside a variable..
console.log(compare) returns undefined for some reason.
Please help
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var result;
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer's choice: " + computerChoice);
console.log("Your choice: " + userChoice);
console.log(computerChoice + " vs " + userChoice);
var compare = function(userChoice, computerChoice){
if (userChoice === computerChoice){
return "The result is a tie!";
} else if (userChoice ==="rock"){
if(computerChoice ==="scissors"){
return "rock wins!";
}
else {
return "paper wins!";
}
} else if (userChoice === "paper") {
if(computerChoice === "rock") {
return "paper wins!";
}
else {
return "scissors wins!";
}
} else if (userChoice === "scissors") {
if(computerChoice === "rock") {
return "rock wins!";
}
else {
return "scissors wins!";
}
}
}
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var result;
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer's choice: " + computerChoice);
console.log("Your choice: " + userChoice);
console.log(computerChoice + " vs " + userChoice);
function compare(userChoice, computerChoice){
if (userChoice === computerChoice){
return "The result is a tie!";
} else if (userChoice ==="rock"){
if(computerChoice ==="scissors"){
return "rock wins!";
}
else {
return "paper wins!";
}
} else if (userChoice === "paper") {
if(computerChoice === "rock") {
return "paper wins!";
}
else {
return "scissors wins!";
}
} else if (userChoice === "scissors") {
if(computerChoice === "rock") {
return "rock wins!";
}
else {
return "scissors wins!";
}
}
}
var Compare = compare(userChoice, computerChoice);
console.log(Compare);
You have syntax error here...
return = "The result is a tie!";
change this to:
return "The result is a tie!";
As Shinra tensei mentioned you had a = in line 18 that caused an error.
Here is a updated snipped. I also added an alert for the compare function.
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var result;
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer's choice: " + computerChoice);
console.log("Your choice: " + userChoice);
console.log(computerChoice + " vs " + userChoice);
alert(compare(userChoice,computerChoice));
function compare (userChoice, computerChoice){
if (userChoice === computerChoice){
return "The result is a tie!";
} else if (userChoice ==="rock"){
if(computerChoice ==="scissors"){
return "rock wins!";
}
else {
return "paper wins!";
}
} else if (userChoice === "paper") {
if(computerChoice === "rock") {
return "paper wins!";
}
else {
return "scissors wins!";
}
} else if (userChoice === "scissors") {
if(computerChoice === "rock") {
return "rock wins!";
}
else {
return "scissors wins!";
}
}
}
The main problem here is that you never executed the anonymous function you wrote. I made compare a normal function instead of an anonymous one, just to make it more similar to other languages. This is a good working code:
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var result;
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer's choice: " + computerChoice);
console.log("Your choice: " + userChoice);
console.log(computerChoice + " vs " + userChoice);
function compare(userChoice, computerChoice){
if (userChoice === computerChoice){
return "The result is a tie!";
} else if (userChoice ==="rock"){
if(computerChoice ==="scissors"){
return "rock wins!";
}
else {
return "paper wins!";
}
} else if (userChoice === "paper") {
if(computerChoice === "rock") {
return "paper wins!";
}
else {
return "scissors wins!";
}
} else if (userChoice === "scissors") {
if(computerChoice === "rock") {
return "rock wins!";
}
else {
return "scissors wins!";
}
}
}
console.log(compare(userChoice, computerChoice));
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>
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 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);
I've included a screenshot image of a codeacademy error I made today. I'm trying to create a compare function that randomly picks a number between 0 and 1 (paper, scissors, or rock) that inputs two choices and returns the winner based on how choice1 compares to choice2.
The first part is a comment, but it explains how the original paper scissors rock function was built
Here is the code:
/*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!");
if (choice1 < 0.34)
if(choice2 ==="scissors");
return("rock wins");
} else if(choice2 ==="paper");{
return("paper wins");
};
};
It's telling me that there's an unexpected token else on line 15 (the else if line)
When I erase the else part it gives me another syntax error saying the same thing about a token if. I'm stuck on what part of my syntax is off and how to fix it.
I have a feeling it has something to do with === and the ; after the if() statements, either way, here is a nicer way of comparing them.
function compare(a,b)
{
if(a==b)return "draw";
switch(a)
{
case "rock":return (b=="scissors"?a:b)+" wins";
case "paper":return (b=="rock"?a:b)+" wins";
case "scissors":return (b=="paper"?a:b)+" wins";
}
}
console.log(compare("scissors","paper"));
function compare(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
}
if (choice1 < 0.34) {
if (choice2 === "scissors") {
return "rock wins";
} else if (choice2 === "paper") {
return "paper wins";
}
}
}
Check the comments on semicolon related errors below.
var compare = function (choice1, choice2) {
if (choice1 === choice2) return("The result is a tie!");
if (choice1 < 0.34) {
if(choice2 === "scissors") { // remove ; here
return("rock wins");
} else if (choice2 === "paper") { // remove ; here
return("paper wins");
} // remove ; here
} // add another else => what happens when choice1 >= 0.34 (not a rock)
};
With the required else blocks the complete function would look like:
var compare = function (choice1, choice2) {
if (choice1 === choice2) return("The result is a tie!");
if (choice1 < 0.34) { // rock
if(choice2 === "scissors") {
return("rock wins");
} else if (choice2 === "paper") {
return("paper wins");
}
} else if (choice <= 0.67) { // paper
if(choice2 === "rock") {
return("paper wins");
} else if (choice2 === "scissors") {
return("scissors wins");
}
} else { // scissors
if(choice2 === "paper") {
return("scissors wins");
} else if (choice2 === "rock") {
return("rock wins");
}
}
};
EDIT
This is just to help you overcome the confusion over semicolons if any. Usually a function definition is not required to have a ; after its body completes by putting the last closing curly brace }.
function compare (choice1, choice2) {
// ...
}
On the contrary, when we assign a variable a value, the statement is terminated by a semicolon.
var name = "John Doe";
Hence, when we combine the two, we're defining a function and then using it in an assignment statement which needs to be closed by using a semicolon. Hence, the syntax:
var compare = function (choice2, choice2) {
// ...
};