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

Can someone help me with this?

I really don't understand what they are asking me to do.

script.js
function max(big, small) {
  return small;
}

3 Answers

Hi Carl,

You need an if statement in the function to check which of the numbers passed to the function is larger

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

The if statement checks to see if a is larger than b and, if it evaluates to true, the function returns a. Otherwise, if b is larger than a or they are equal, the else statement returns b.

Hope this helps.

simhub
simhub
26,544 Points

hi Carl,
they want you to use a conditional statement:
you can try this:

function max(a,b){
return (a>b)?a:(b>a)?b:null;
}

It's a if else shorthand statement.

I'm sorry, but this dos not help me. Some of the syntax you are using has not been taught in the course yet. I still don't understand.

simhub
simhub
26,544 Points

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

function max(a,b){
  if(a>b){ // see if a greater then b, if so it returns true
    return a // if true return a
  }else if(b>a){ // else see if b greater then a, if so it returns true
    return b // if true return b
  }
}

Dear People, does anyone forget to parse the values.