Asim Mahar
Asim Mahar

Reputation: 1410

Javascript Return and if/else

For the following code, I keep getting Scissors back for alert. I am not sure what am I doing wrong here.

var computerChoice = Math.random();

var newChoice = function (computerChoice) {
    if (computerChoice <= 0.34) {
        var newChoice = "rock";
        return newChoice;
    } else if ((computerChoice >= 0.35) && (computerChoice <= 0.66)) {
        var newChoice = "paper";
        return newChoice;
    } else {
        var newChoice = "scissors";
        return newChoice;
    }

}
var newerChoice = newChoice();
alert(newerChoice);

Upvotes: 3

Views: 108

Answers (7)

MaxZoom
MaxZoom

Reputation: 7753

The computerChoice argument to the newChoice method is undefined as nothing is passed in the call newChoice(). If you want to pass previously generated number, you could do that as below:

var computerChoice = Math.random();

var newChoice = function(computerChoice) {
  if (computerChoice <= 0.34) {
    return "rock";
  } else if ((computerChoice >= 0.35) && (computerChoice <= 0.66)) {
    return "paper";
  } else {
    return "scissors";
  }
}
var newerChoice = newChoice(computerChoice);
alert(newerChoice);

Upvotes: 1

Rizky Fakkel
Rizky Fakkel

Reputation: 9183

Have a good look at this line:

var newChoice = function (computerChoice) {

You created a function called newChoice, and require an argument called computerChoice. Now let's see how you execute that function later on.

var newerChoice = newChoice();

You execute the function newChoice, but you did not pass computerChoice as an argument this time.

The solution

What you can do is fairly easy. Change the following:

var newChoice = function (computerChoice) {

Into

var newChoice = function () {

And make it so that your newChoice function generates a new value for computerChoice, by adding computerChoice = Math.random(); to your function. Like this:

var computerChoice = Math.random();

var newChoice = function () {
computerChoice = Math.random();
    if (computerChoice <= 0.34) {
        var computerChoice = "rock";
        return newChoice;
    } else if ((computerChoice >= 0.35) && (computerChoice <= 0.66)) {
        var newChoice = "paper";
        return newChoice;
    } else {
        var newChoice = "scissors";
        return newChoice;
    }

}
var newerChoice = newChoice();
alert(newerChoice);

Upvotes: -1

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

There appears to be a bit of confusion over variables and parameters. Here's some working code...

var newChoice = function (choice) {
    if (choice <= 0.34) {
        return "rock";
    } else if (choice <= 0.66) {
        return "paper";
    } else {
        return "scissors";
    }
};

var computerChoice = Math.random();
var newerChoice = newChoice(computerChoice);
alert(newerChoice);

The value that is passed into newChoice is the parameter choice. I trimmed out the && as they're not required. If choice is not <= 0.34 then it must be greater than 0.34, so that check is not required later.

computerChoice is a random value variable that is passed as a parameter to the function.

Here's a working fiddle example...

http://jsfiddle.net/ArchersFiddle/0dfhoa63/

Upvotes: 1

leopik
leopik

Reputation: 2351

Your newChoice function is expecting a parameter. Therefore you ened to give it one

var newerChoice = newChoice(computerChoice);

Also, I believe that in the first if statement, you wanted to have newChoice = "rock";, not computerChoice = "rock";

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816442

You are not passing an argument to newChoice when you call it, hence the value of the parameter computerChoice is undefined. undefined <= 0.34 is false, same for the other comparisons.

Two possible solutions are:

  • Remove the parameter from the function, so that computerChoice refers to the global variable. Currently the parameter shadows the outer variable with the same name.
  • (better) Call the function with an argument.

Learn more about functions.

Upvotes: 2

Liam MacDonald
Liam MacDonald

Reputation: 301

You aren't actually passing in computerChoice as a variable to the function despite declaring it as an argument. Instead of var newerChoice = newChoice(); do var newerChoice = newChoice(computerChoice);. I think the function will be looking for computerChoice as an argument instead of looking at the earlier declaration. Look up variable scope in javascript.

Upvotes: 0

Randy Hunt
Randy Hunt

Reputation: 425

var newerChoice = newChoice(Math.random());

Upvotes: -1

Related Questions