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

Having difficulty putting together a function with a conditional statement.

I don't know if I am on the right steps on this question or not but I am pretty stumped as to where to go next.

script.js
function max ( 1,2 ){

}

if (1 < 2) { 
  return true;
} else {
  return false;
}

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Tyler, You are on the right track, but the challenge want you to pass in arguments and return the larger of the two. You are returning a boolean value. So, your structure and syntax is correct, you just need to change some values and variables. And the conditional statement needs to be inside the function.

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

When you call the function and put in the number arguments, the conditional statement will compare the 2 numbers and return the one that is larger. eg. a = 5 and b = 6... the "IF" part will return false, so it will move on to the "ELSE" part which evaluates to True and therefore will return b (or in this case 6).

I hope that helps you. Keep Coding! :)