Yet another Game created using JavaScript on Codecademy.com – Rock, Paper & Scissors

Here goes another adventure tale with codecademy.com

Just finished another game project as part of my JavaScript learning track.
This time it is Rock, Paper, Scissors game, built using only the very basic knowledge of conditional flows / JavaScript Syntax gained through Codecademy’s JavaScript learning track so far.

Check out my Codecademy profile to see how I am progressing on my Code Learning Ventures…

Here is how the source code looks for this game:

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

compare (userChoice, computerChoice);
Advertisements
This entry was posted in Code Academy, JavaScript and tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

4 Responses to Yet another Game created using JavaScript on Codecademy.com – Rock, Paper & Scissors

  1. kalez618 says:

    same code but getting an error

    • Hi,

      I tried this code in the Sandbox compiler of Codecademy.com It might be that certain functions / libraries do not work across all the compilers / IDEs that you might be using to compile the code.
      If you could post the exact error (may be a screenshot of error) that you see while compiling the code, I may be able to help.

      Thanks
      Deep

  2. Jansen Nj says:

    thanks i got stucked on the game!

  3. bill says:

    i have two questions!
    1)is Math.random an already made function??
    2)are the possibilities for the computer to choose rock paper or scissors equal??
    thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s