0

I am new to js, I just wrote the basic function below based on the rock, paper, scissors game. For some reason the result of the compare function is always showing up as a "draw" rather than the other results. What am I doing wrong here?

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";
}

choice1 = userChoice;
choice2 = computerChoice;

var compare = function (choice1, choice2) {
if (choice1 == choice2) {
        return "draw!";
}

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

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

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

compare();

Thanks, Us

UsmanA
  • 61
  • 1
  • 7
  • 3
    You're not passing any arguments to `compare`. – James Allardice Jun 24 '13 at 10:10
  • chek what random value it gives each time aand the returning value of the user choise – Alessandro Gabrielli Jun 24 '13 at 10:11
  • By the way, it's better to write var computerChoice = Math.random() * 3 and compare computerChoice in [0, 1) for rock, [1, 2) for paper and remainder for scissors: more precise. – Bathsheba Jun 24 '13 at 10:12
  • by the way, i think you should use a matrix to determine the winner... imagine that you extend the game to `rock paper scissors lizard spock` (TBBT copyright), you'll have to completely rewrite you code... – pataluc Jun 24 '13 at 10:12
  • either you remove arguments from compare function or pass the arguments when calling it – chetan Jun 24 '13 at 10:15
  • Thanks everyone! @pataluc - Can you show an example of how you do this? – UsmanA Jun 24 '13 at 11:54

3 Answers3

4

You're calling compare without parameters:

compare();

Therefore choice1 and choice2 both equals undefined and your game will always end up as draw. You should try calling your compare function like this:

compare(userChoice, computerChoice);

If you define a function, the parameter list defines the names of the given variables within the scope of the function. It's not a naming convention for the variables which should be available in the function itself.

devsheeep
  • 2,076
  • 1
  • 22
  • 31
0

You have defined the function with two arguments:

var compare = function (choice1, choice2) 

However you have invoked it with 0.

Try specifying the choices:

compare("rock", "paper");
Darren
  • 68,902
  • 24
  • 138
  • 144
0

You can't turn on function only by typing func_name() without arguments, thats like "dry shot". Read about function declaring

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";
}
choice1 = userChoice;
choice2 = computerChoice;
function compare (choice1, choice2) {
  if (choice1 == choice2) {
    return "draw!";
  };
  if (choice1 == "rock") {
    if (choice2 == "scissors") {
      return "rock wins!";
    } else {
      return "paper wins!";
    }
  }
  if (choice1 == "paper") {
    if (choice2 == "scissors") {
      return "scissors wins!";
    } else {
      return "paper wins!";
    }
  }
  if (choice1 == "scissors") {
    if (choice2 == "rock") {
      return "rock wins!";
    } else {
      return "scissors wins!";
    }
  }
};
compare(choice1, choice2);
Community
  • 1
  • 1
khex
  • 2,778
  • 6
  • 32
  • 56