Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Create a max() Function

DJ J
PLUS
DJ J
Courses Plus Student 1,960 Points

Not returning a number?

function max(int1, int2) { if (int1 > int2) { return int1; } } max(10000,13);

Receiving an error that my code is NOT returning a number.

script.js
function max(int1, int2) {
  if (int1 > int2) {
    return int1;
  }
}
  max(10000,13);

3 Answers

You need to return a number regardless of if the 1st number is larger.

function max(int1, int2) {
  if (int1 > int2) {
    return int1;
  } else {
    return int2;
  }
};
max(10000,13);
DJ J
DJ J
Courses Plus Student 1,960 Points

The exercise asked for the larger number to be returned, my function is returning the larger number. I checked it in the chrome console, but I am still being told that I am not returning a number.

DJ J
PLUS
DJ J
Courses Plus Student 1,960 Points

Apparently, this was the correct way. Why I don't really understand...

function max(int1, int2) {
  if (int1 > int2) {
  }
  return int2;
}
max(10000,13);

It might have passed the challenge but as long as you understand why this wouldn't be best practice in a real world project then you should be fine. Code Challenges here are weird sometimes. haha

DJ J
PLUS
DJ J
Courses Plus Student 1,960 Points

Yeah you're right lol. Quick question, how did you include the code in the code box like that?

*How to Post Code