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

Zachary Pratt
Zachary Pratt
2,395 Points

How to Creat the Max Function with a Conditional Statement

I am not sure what I'm missing here.

script.js
function max(1, 2) {
  if (1 > 2)
       Return 1;
} 
  if (2 > 1) 
       Return 2;
}
}

2 Answers

Hi Zachary,

It seems there was a couple of things going a miss but nothing major; firstly you were passing the number in the original functions declaration and the question wanted you to make a function that "accepted" two numbers. So for this you just need two place holders.

Secondly you had wrote out two "if's" and you needed and "if else" :)

The coded below will get you through the challenge and this is a good quick overview of javascript functions. (I had to look here to jog my memory)

function max($i, $e) {

  if ( $i > $e ) { 
    return $i;
  } else { 
    return $e;
  }

}
alert ( max( 1, 2 ) );

Hope this helps Craig

Zachary Pratt
Zachary Pratt
2,395 Points

This did not work either

Zachary Pratt
Zachary Pratt
2,395 Points

Thanks Craig. My error. It did work! This is the Best Answer!

akak
akak
29,445 Points

You are missing opening curly brackets after each if statement :)