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

The `max` function isn't returning a number. (but really it is)

I have no doubt this isn't the most elegant code out there, but I've run it and it returns a number. so I'm not sure why I keep getting an error in the embedded editor. Would appreciate any help. I'm sure I'm just making a really silly mistake.

function max(num1, num2) { if (parseInt(num1) > parseInt(num2)) { return parseInt(num1); } }

document.write("<h1>" + (max(35, 22)) + "</h1>");

script.js
function max(num1, num2) {
  if (parseInt(num1) > parseInt(num2)) {
    return parseInt(num1);
  }
}

max(35, 22);

3 Answers

You're returning num1 when num1 is greater than num2, but youre not returning anything if num2 is greater than num1.

Let me know if this helps here

Got it. Thanks Adam.

Thanks for the help.

That's OK though. an if statement doesn't always have to be followed by an else. but does need to have code to run if the condition is true (which in this case it does and it is)....when I called the function with max(35, 22) it should return 35 and does when I run it in the console...?

You need to account for the scenario I mentioned, tho. Say I called your func like this:

max(22,35);
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

You would be correct here, except the challenge is sending in numbers. You are not meant to call the function yourself. That being said, Adam is correct. The challenge should return the max of any two values no matter which two numbers are sent in and in which order. Your code only returns the max if the first argument was greater than the second.

I Dilate
I Dilate
3,983 Points

It may be returning an error because of the extra brackets you've inserted in your document.write...

document.write("<h1>" + (max(35, 22)) + "</h1>");

should be:

document.write("<h1>" + max(35,22) + "</h1>");