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

Functions

Having a hard time figuring out functions, especially when it comes to multiple arguments and figuring out how and what to return...

script.js
function max( number1 , number2 ) {
  return max;
}

2 Answers

Julian Aramburu
Julian Aramburu
11,368 Points

Hi Bradley! In this particularly case you are asked to build a function called max that accepts two arguments(that would be numbers), you've done that already! Good job! Now the function should return the larger number between the two..you are hinted to use conditional statements, are you familiar with those? In this case you could use an If statement to check if the first argument is larger than the second argument and if it is, return that first argument or else return the second argument. See if you could come to a solution with this hint! If you can't let me know!

Hope you find this answer helpful!

Cheers and Keep Coding!

I was looking at doing something like this...

function max( number1 , number2 ) {
  var higherNumber = Math.max( number1 , number2);
  return higherNumber;
}

console.log( max(2,4) );
Julian Aramburu
Julian Aramburu
11,368 Points

yeah! That's also a good way to do it :D! and you don't even need to set a variable , you just return Math.max(number1, number2) and that's it!

Cheers!

Keep Coding mate!