IronNoob

Please login or register.

Login with username, password and session length
Advanced search  

News:

Migrated to dedicated hosting. Post any weirdness you observe in the Lounge.

Author Topic: Can't Figure out one of the JavaScript Exercises on CodeAcademy  (Read 164 times)

0 Members and 1 Guest are viewing this topic.

tart3rsauc3

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 279
  • Christian,Musician (Vocal and Cello)/Game Designer
    • View Profile

I'm making a rock paper scissors game. Apparently my first compare function isn't working correctly. I've looked over it a bazillion times and I don't understand why it still isn't right. The exercise always says, "Your compare function doesn't return the correct string when comparing x to y".

Quote
var compare = function(choice1, choice2) {
   choice1 = userChoice;
   choice2 = computerChoice;
   if(choice1 == choice2) {
      return "The result is a tie!";
   }
   else if(choice1 == "rock") {
      if(choice2 == "scissors") {
      return "rock wins";
      } else {
         return "paper wins";
      }
   }
};
Logged
I CHANGED MY 3 LIFE TIPS
1. Don't limit yourself. Saying you can't do something automatically makes it true in spirit. So stop.
2. Try lots of new things. Be an early adopter.
3. Women are weird. Just start a conversation with them to see what's up if it's bugging you so much. You never know.

mustyoshi

  • Hero Member
  • *****
  • Offline Offline
  • Posts: 3099
  • I'm a pig, your argument is invalid.
    • View Profile
Re: Can't Figure out one of the JavaScript Exercises on CodeAcademy
« Reply #1 on: March 06, 2013, 12:27:23 pm »

Why are you setting choice1 and choice2 inside the function?

Also, you only check if choice1 is rock, you need to check if choice1 is paper or scissors.

Also, if you wanted to be real efficient like. A better way to do it would be to make choice1 and choice2 numbers
0 = paper
1 = rock
2 = scissors

You'll notice the next one in the list (if it wrapped around) is the one that is beaten by it's previous.

result = (choice1 - choice2)
So if results is less than 0 player 1 lost, if it's 0, then it was a tie, and if its greater than 0, player 1 won.
Logged

Act of God, insurance companies don't cover it.
[/m

cgm

  • Hero Member
  • *****
  • Offline Offline
  • Posts: 937
    • View Profile
Re: Can't Figure out one of the JavaScript Exercises on CodeAcademy
« Reply #2 on: March 06, 2013, 04:57:05 pm »

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";
}
/*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 === "rock")
{
if (choice2 === "scissors")
{
return "rock wins";
} else {
return "paper wins";
}}
if (choice1 === "paper")
{
if (choice2 === "rock")
{
return "paper wins";
} else{
return "scissors wins";
}}
if (choice1 === "scissors")
{
if (choice2 === "rock")
{
return "rock wins";
}else
{
return "scissors wins";
}
}
};
compare(userChoice,computerChoice);
Logged

tart3rsauc3

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 279
  • Christian,Musician (Vocal and Cello)/Game Designer
    • View Profile
Re: Can't Figure out one of the JavaScript Exercises on CodeAcademy
« Reply #3 on: March 06, 2013, 07:17:37 pm »

Why are you setting choice1 and choice2 inside the function?
This. This made my function work correctly. Haha.

It was a code snippet. Adding in checks for if choice1 is paper or scissors comes later in the exercise. It'll be like what cgm posted.
Thank you.
Logged
I CHANGED MY 3 LIFE TIPS
1. Don't limit yourself. Saying you can't do something automatically makes it true in spirit. So stop.
2. Try lots of new things. Be an early adopter.
3. Women are weird. Just start a conversation with them to see what's up if it's bugging you so much. You never know.

k9rosie

  • Hero Member
  • *****
  • Offline Offline
  • Posts: 871
    • View Profile
Re: Can't Figure out one of the JavaScript Exercises on CodeAcademy
« Reply #4 on: March 06, 2013, 07:38:18 pm »

Here, I wrote this for you:
http://jsfiddle.net/wey5S/2/

You already figured it out but I'm sharing it with you anyway so you can read the mini documentation I put on there.

Here is the code without docs:
http://jsfiddle.net/wey5S/3/
Logged

tart3rsauc3

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 279
  • Christian,Musician (Vocal and Cello)/Game Designer
    • View Profile
Re: Can't Figure out one of the JavaScript Exercises on CodeAcademy
« Reply #5 on: March 13, 2013, 05:10:19 pm »

Here, I wrote this for you:
http://jsfiddle.net/wey5S/2/

You already figured it out but I'm sharing it with you anyway so you can read the mini documentation I put on there.

Here is the code without docs:
http://jsfiddle.net/wey5S/3/

Thank you!
This helped me understand a lot.
Logged
I CHANGED MY 3 LIFE TIPS
1. Don't limit yourself. Saying you can't do something automatically makes it true in spirit. So stop.
2. Try lots of new things. Be an early adopter.
3. Women are weird. Just start a conversation with them to see what's up if it's bugging you so much. You never know.
 

Page created in 0.103 seconds with 21 queries.