3

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);
1
  • 3
    You are not passing an argument to newChoice when you call it, hence computerChoice (the parameter of the function) is undefined. undefined <= 0.34 is false, same for the other comparisons. Commented Apr 8, 2015 at 15:05

7 Answers 7

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

There is also third solution to generate the number inside the function, so nothing needs to be passed: var newChoice = function() { var computerChoice = Math.random();
Thanks Felix, completely forgot about the passing the argument. And referring to global variable and undefined helped very much-:)
@MaxZoom: True. I didn't mean to imply that there are only these two solutions.
@Max: Thanks for the third solution, I just ran it for conceptually understanding it. And of course it worked-:) The variable computerChoice was incorrectly put in there due to typing, nonetheless thanks for pointing it out.
@MaxZoom, I have edited var computerChoice to var newChoice in the first if statment.
1

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/

Comments

1

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);

3 Comments

"method is null" -> "method is undefined"
Sorry, I was (badly) quoting part your answer. The argument is not null, it is undefined.
Why should I? Maybe I still haven't expressed myself clearly: You wrote: "The computerChoice argument to the newChoice method is null." However, that is incorrect. When no value is passed, the value of the argument is undefined, not null. Hence it is more correct to say "The computerChoice argument to the newChoice method has the value undefined."
0

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.

Comments

0

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

Comments

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

2 Comments

Could you please explain your answer.
The function expects a parameter to be passed in.
-1

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);

1 Comment

I learn as I go, so I'd appreciate it if someone elaborates why I'm being downvoted when they do downvote me. Thanks in advance folks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.