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

idriss Elouilani
idriss Elouilani
Courses Plus Student 1,232 Points

I create the first function max but is not working

function max (10,6){ var twoNumber = larger of the two numbers; if (the first number >the second number); return true}else {return false }

function max (10,6){
  var  twoNumbers = larger of the two numbers;
  if ( the first number> the second numberlse
}else{ return false;

3 Answers

Bryan Knight
Bryan Knight
34,214 Points

It could look something like this:

function max(num1, num2){
  if(num1 > num2){
    return num1;
  } else {
    return num2;

You take in two arguments (here I call them num1 and num2). Your conditional checks to see if num1 is greater than num2. If it is then you'll return num1 otherwise (else) return num2.

if you want to get a little fancy with a one liner (ternary) you can do this:

function max(num1, num2) {
return num1 > num2 ? num1 : num2;

You want functions to be reusable so your parameters can be called whatever you want.

function max (x,y){
  //see if x is larger than y
  if (x>y) {
   //if it is return x
    return x
  } else {
   //otherwise return y 
    return y;

//Now because it is a function I can reuse it wherever I'd like keeping your code DRY
//returns 6

//returns 10

//returns 10