Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am a new programmer and found this site. This will be my first question on this site so I am sorry if this question is not the best. I am learning Javascript on a website called codecademy. As I was going through a project about making rock paper scissors with javascript, I came upon a problem.
If you would like to find the questions that codecademy asks, just search it up. Anyways, if you could, I would like you to help me debug my code because I am quite new to programming and would like some help because I don't understand where I went wrong. Please remember that I am new and I will not be able to understand your answers if you go too deep, make it "simple" for me if you could. Thank you for your time, and sorry for my huge wall of text!
------------------------------------------------------------------------------------
Here is the code I have:

C#
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";
    }
    else if(choice1 === "scissors") {
        if(choice2 === "rock") {
            return "rock wins";
        }
    else {
        return "scissors wins";
    }
    }

}

------------------------------------------------------------------------------------
When I run my code, the console responds with unexpected token else. Also, if you see anymore problems, please put down a solution to those problems too. Thank you so much for your time and I would be grateful if you can figure out where I went wrong!

Happy coding everyone!
Posted
Comments
Sergey Alexandrovich Kryukov 2-Jan-15 21:40pm    
"Search it up"? You don't even want to give a link and still expect someone would like to help you?
Okay, here is the first hint? When you repeat some constant, such as "rock", at least twice, the hole code is good for nothing, it is not maintainable. Declare all those words as string objects in one place.
—SA
UWStudents 3-Jan-15 10:03am    
Sorry for not putting down the link, my bad. The instructions on codecademy say that I should do it like this, they show me the format of it and I just have to put it into code. Maybe it was just me, but it seemed to me the instructions on codecademy wanted me to do my code in that format.

To answer your question: to help yourself to debug the code, use the debugger. You will solve two problems instead of one: debug your code and learn how to debug.

Now, please see my comment to the question. In addition, I'll say this: it's also pretty bad to use strings to represent some state. This very simple and useful article will give you much better idea:
http://stravid.com/en/cleaner-javascript-code-with-enums[^].

According to this idea, you could have this object to represent the state of each move:
JavaScript
var Moves = {
    Rock: 0,
    Paper: 1,
    Scissors: 2
};
and use it as explained in the article. The idea of the game is to use the non-transitive relationship between the three. Define it explicitly: define a predicate function objects which accept two Moves values and returns Boolean (which one wins) and use it in your game.

To use it well, it would be much better not to use the prompt. Instead, you could write 3 buttons, "Rock", "Paper" and "Scissors". Click event handler on each button would call some function with the parameter equal to Moves.Rock, Moves.Paper, etc.

I think I explained all important key points to you. Remaining part is knowledge of Javascript and programming basics, and some debugging skills. Changes are, if you write this code again in a cultured way, it may require very little to no debugging.

[EDIT]

Also, using console.log is hardly useful or acceptable for a game; it will show text on a special log window, not in a Web page. You could use some HTML element (find it, say, using document.getElementById) and add child element (text nodes or whatever else) to it.

[END EDIT]

Good luck. Happy New Year!

—SA
 
Share this answer
 
v2
Comments
DamithSL 2-Jan-15 22:14pm    
SA, Wish You a Happy New Year 2015! :)
thanks for the advices, 5+
Sergey Alexandrovich Kryukov 2-Jan-15 22:32pm    
Thank you, Happy New Year!
—SA
In addition to SA's Answer[^], you have incorrect if else(..) else (..) if else (..) code
check
JavaScript
else if(choice1 === "paper") {
        if(choice2 === "rock") {
            return "paper wins";
        }
    }
    else {
        return "scissors wins";
    }else if(...)


it should be
JavaScript
else if(choice1 === "paper") {
        if(choice2 === "rock") {
            return "paper wins";
        }
    else {
        return "scissors wins";
    }
} else if(...)

sample code: (only fix the issue in your code, you better use enums as SA suggested.)
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("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(choice1 ==="scissors")
    {
        if(choice2 === "rock")
        {
            return "rock wins";
        }else
        {
            return "scissors wins";
        }
    }else
    {
         return "incorrect input";
    }
};
alert(compare(userChoice,computerChoice));
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 2-Jan-15 22:36pm    
Not quite "in addition": I tried to warn against using such immediate constants, which is not maintainable, and string comparison, which is not really needed.
Besides, console.log will log data not on the page but to a special log console, which is hardly acceptable for a game (I'll add this item to my answer).
—SA
DamithSL 2-Jan-15 23:30pm    
CodeCademy site provide that special console that's why I didn't worry about it. Anyway my code will not run with OP's code which having incorrect if else code. answer updated, OP will at least get idea on "if else syntax"
UWStudents 3-Jan-15 10:09am    
I still have not learned about the "alert" command, but it still works as an answer. I'm trying to understand the sample code you gave me right now :/, because I don't want to just pass that section without understanding what I did. Thank you guys for the answers and I am sorry if my knowledge on javascript is too little. Do you guys recommend me using this site again if I need help debugging my own code? Also, does it matter where I put the { and }?
For example:

if(???) {
//do stuff
}
------------
if(???) // in the previous way, the { was here, but now it's under the if statement.
{
do stuff
}
--------------------
I was wondering if by putting the { and }'s in a specific place, it will change the outcome of the code. Just wondering.
The problem I found is that after where you have the return command you need parenthesis around what you wish to return so it would look like this: return (What is being returned)
Also you have some unnecessary else's

Hope this helps!
 
Share this answer
 
Your code should look like this.

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("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(choice1 === "scissors") {

    if(choice2 === "paper") {
        return "scissors wins";
    }
    else {
        return "rock wins";
    }

}



};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900