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!

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

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.