This forum is now read-only. Please use our new forums! Go to forums
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
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
Answer 5062021f8b5cbf0002000c9c
@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.
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
1 comments
May I ask why var square = function (x) { return x * x; };
var cube = function (x) {
return (square * x);
}; does not work?