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

Josh Wozniacka-Knoll
Josh Wozniacka-Knoll
8,796 Points

I'm stuck. I cant figure out what I am doing wrong.

Create a new function named max which accepts two numbers as arguments (you can name the arguments, whatever you would like). The function should return the larger of the two numbers.

HINT: You'll need to use a conditional statement to test the 2 parameters to see which is the larger of the two.

What am I doing wrong here??

script.js
function max (32, 28) {
  return 32;
}
if ( parseInt(32) > parseInt(28) ) {
    correct = parseInt(32);
  }

1 Answer

First, you can't name a parameter with the first character as a number. Remember: A parameter is basically a variable!

Second, you should only write code in the function, no code outside!!

Also, if the first argument is bigger then the second argument, great, return the first argument, otherwise return the second argument. You did the first part of returning the first argument, but I don't see anywhere you returning the second argument if the second argument is bigger!

If you fix all of these, you should end up with:

function max(a, b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
}

:warning: Remember not to call the function!! The challenge does that for you.

I hope this helps :grin:

:dizzy: ~Alex :dizzy:

Josh Wozniacka-Knoll
Josh Wozniacka-Knoll
8,796 Points

Do I have to write parseInt in front of the numbers. because when i do this it says bummer there was an error with your code SyntaxError, Parse error.

Josh Wozniacka-Knoll
Josh Wozniacka-Knoll
8,796 Points

nevermind I was reading the task 'Create a new function named max which accepts "two numbers" as arguments'. And thinking that i needed to have two numbers. Thank you for your help!!!