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

challenge # 1

syntax error

script.js
function max(20, 15){

  if (20 > 15) {
      return 20;
  }
  else{
      return 15;
  }
}

1 Answer

rydavim
rydavim
18,813 Points

You've got the general structure, but it's a little bit more tricky than that. They don't want you to hard code the numbers in there, since that isn't very useful. Instead, you want to write a function that will return the larger of any two integers you pass.

function max(num1, num2) { // This is the same function you have, but with variables instead of hard coded numbers.
  if (num1 > num2) { // You've got the right idea, but use the variable names instead.
    return num1;
  } else {
    return num2;
  }
}
max(13, 7); // Here's where you call your new function with any two numbers you like.

It's worth noting that you don't have to include an edge case for if the two numbers are the same. Let me know if you have any more questions. Happy coding!