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

Vivienne Kay
Vivienne Kay
1,708 Points

Stuck trying to pass number values into a function

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

max(5, 8);

alert( max( ) ):

The alert is asking me if I passed the numbers correctly into the max function. Not sure what I'm doing wrong.

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

max(5, 8);

alert( max( ) );
Jean Mesa
Jean Mesa
2,251 Points

replace the colon with a semi-colon on your last line

2 Answers

Jonathan Dewitt
Jonathan Dewitt
8,101 Points

1) You have a bit too much logic. You don't need the second if statement, as you only have two outcomes.

2) You should be passing the values directly into the function within the alert

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

alert( max(5, 8) );

Alternatively, you could put the alerts directly into the function and then run it by itself:

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

max(5, 8);
Vivienne Kay
Vivienne Kay
1,708 Points

Wow! Thank you so much! This fixed the problem and also helped me see how to write cleaner conditional statements in the future. ?

Jean Mesa
Jean Mesa
2,251 Points

replace the colon with a semi-colon on your last line