Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

I want to check which of the two numbers is larger, then return it. How do I do that?

I'm trying to create a function that checks which of two arguments is larger, then returns it. I'm not sure how to proceed.

function max(a+b) {
  if(a<b) {
  } else(b>a) {

You seem to have the general idea, but your syntax could use some work. When setting parameters, you want to separate them by a comma. Also, in this case, you don't ever define your arguments, so I have no idea why you want to return the integer five or four in either case. And finally, the else statement is for all other cases than (a < b), so further elaboration is unnecessary.

I would structure it this way:

var a = 5;
var b = 4;

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

You could also set your arguments inside your function, but you would need to set values to a and b or you would be comparing two undefined variables.

1 Answer

Thank you so much, Eric! I figured it was something silly that I'd messed up on.