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

Aaron Noble
Aaron Noble
6,559 Points

Create a new function named max which accepts two numbers as arguments (you can name the arguments, whatever you would l

I'm not sure what I'm doing wrong. I've declared a function with 2 argument. I then compare the arguments in a conditional statement and returned a string. I then called the function. Is there a syntax error going on here? Thank you for any input.

Aaron :)

script.js
function max (num1, num2) {
    if ( num1 > num2 ) {
    return num1 + " is smaller than " + num2;
  } else ( num1 < num2 ) {
    return num1 + " is larger than " + num2;
}  
 max(2,3);

3 Answers

Aaron Noble
Aaron Noble
6,559 Points

oh wow, that looks and makes a lot of sense. Thank you for the fast reply. I'm going to give it a try!

Is it possible to use the anonymous function then using the named? I'm not quite sure when to use it.

 var max = function(num1, num2) {
    if(num1 > num2) {
       return num1;
    } else {
      return num2;
  }
}

Im not to sure what's the different between the anonymous function and the named function

A few things:

(1) If num1 > num2 then num1 would be larger (not smaller). Same logic in the second return statement

(2) However the task asks you to return a number - the greater of the arguments. You are returning strings

(3) You are missing a closing bracket for your function. The corrected code is as follows:

function max (num1, num2) {
    if ( num1 > num2 ) {
      return num1 ;
   }
   else  {
      return num2;
   }  
} // <---- missing