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:
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