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

functions

It says that I have not created a function named max and I believe I have.

script.js
function max(a, b) {
  max = (5, 9)

  if (5 > 9) {
   return( max ) 
  } else {
   return( max ) 
  }
}

max()

3 Answers

Colin Bell
Colin Bell
29,679 Points

You've created a function that takes two parameters - a & b. But you're trying to pass arguments inside the function. You just want to check generally which of the two parameters is the larger number. So if a > b you want to return a, otherwise return b (Keep in mind that this would also return b if they were equal).

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

Then you'd pass the arguments when you call it:

max(5, 9);

Thank you so much I knew I was close but I just couldn't get over that hump?

Michael Hall
PLUS
Michael Hall
Courses Plus Student 30,909 Points

You have created a function called max. The problem is in the call. You just need to add an ' ; ' at the end. Like this:

max(); 

Thank you.

Zoltán Holik
Zoltán Holik
3,401 Points
function max(a, b) {

  if (a > b) {
   return  a + " greater than " + b;
  } else {
   return  a + " not greater than " + b;
  }
}

var result = max(5, 9);
alert(result);