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

Need Help: My Max Function isn't working. Tried different ways for a few days now

script.js
function max (arg2, arg4) {
 if (2 < 4) }
  return 2; 
}

  max(2,4); //Return2

2 Answers

There is a few problems:

Your curly braces for the conditional are messed up, should look like this:

if (2 < 4) {
  return 2;
}

But the main problem is that you are hard-coding 2 and 4 in your function and since 2 is always less than 4 your conditional passes and it returns 2 like you wrote (this isn't what a max function should do anyway; it should be returning the larger number), so when you pass in arguments of 2 and 4 to the function is doesn't matter since you never use them, you're just using the 2 < 4 in the conditional.

You need to accept two parameters, check to see if one is greater than the other, if it is return that value, otherwise the second number is greater and you should return that one.

function max (first, second) {
 if (first > second) {
  return first; 
 }
  return second;
}

max(2,4); //Return2

Thank you for clarifying The lectures and challenges seem to change and leave a bit of confusion as to what should be applied to the new challenges. It also called for alert whereas others used the console. Thanks Again!