Skip to Content
This forum is now read-only. Please use our new forums at discuss.codecademy.com.
0 points
over 8 years

I am getting a NaN error. What does this mean?

when I call the value of square “console.log(square(2))” I get a logical out put: 4. So I know that part of my code works. However, when I change it to print “console.log(cube(4))” all of a sudden I get an error “ NaN. So the code must be breaking down here: ( the section I put in bold) But I just don’t understand why. Any help would be appreciated, thanks!

var square = function (x) { return x * x; };

var cube = function (x) { return x * square; }; console.log(cube(2));

Answer 503252ba1f1459000204bc27

5 votes

Permalink

Nan means “Not a number”, this is because inside your cube function, you’re not calling the square function, but getting it’s contents.

Change return x * square; with return x * square(x); and it should work.

- Nico

4012 points
Submitted by Nico Ekkart
over 8 years

1 comments

Michael Donovan over 8 years

May I ask why var square = function (x) { return x * x; };

var cube = function (x) {

return (square * x);

}; does not work?

Answer 5062021f8b5cbf0002000c9c

-3 votes

Permalink

@Michael I would assume it is the same reasoning that @Nico just gave. Square is a function, so if you call just square you’re calling that entire function and not an actual value. Square(x) passes a value through the parameter which is why that would work. So essentially, (square * x); is saying..

(square = function(x) { return x * x; }; * x)

where square(3), x now equals 3 and it can do the math.

Hope that makes a bit more sense.

770 points
Submitted by Kevin Kendle
over 8 years