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

Can someone please help me with this challenge.?

Can someone please help me with this challenge.? I can't get my head around it.

script.js
function max (20,10){
  if(20 > 10){
  return true;}
  else {return false};
}

1 Answer

  • Your code is syntactically wrong:
function max (20,10) {
  if (20 > 10) {
    return true;
  } else {
    return false;
  };
};

Your goal is to return the larger of two numbers. That is, if you have two numbers a and b in general, your function max needs to return either a or b, whichever is larger. With this in mind, there are several problems with your code:

  • max is not taking 2 arbitrary numbers. If you pass 20 and 10 to your function, since 20 is always greater than 10, your function will always return true. To fix this, we should replace 20 and 10 with 2 generic names, such as a and b:
function max(a, b) {
  if ( a > b ) {
    // the rest of the code
  • The function needs to return either a or b. Right now, your function returns either true or false. To fix this, simply replace them with either a or b:
...
  if (a > b) {
    return a;
  } else {
    return b;
  }
...