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

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

I did

script.js
function max( var x , var y ) { 
if ( x > y ) {
    document.write("X is larger than Y");
    return var x;
} else {
    document.write("Y is larger than x");
    return var y;
}
}
max( 10 , 9 );

2 Answers

Dan Weru
Dan Weru
47,649 Points

Hey,

You're doing great ... you just need to correct your code a little bit. We do not use the var, let or const keywords before JavaScript argument(s).

In the same way, we do not use either of these keywords in the return statements. I understand that explanation could leave you with a few questions. For example, you could ask why are we not declaring the variables x and y with var keyword?. They are declared implicitly by the JavaScript engine, thus we do not need to declare them explicitly with the keyword var.

Your code ought to have been as follows:

function max( x , y ) { 
  if ( x > y ) {
    document.write("X is larger than Y");
    return x;
  } else {
    document.write("Y is larger than x");
    return y;
  }
}
max( 10 , 9 );
Jacob Chapa
Jacob Chapa
1,522 Points

Although the code can technically work it can be shortened up and wrote smaller. Take a look at my code below. You can enter any two numbers into the function 'max' and it will always let you know which number is bigger. Hope this helps.

function max(argument, argument2){
  if (argument > argument2) {
    return argument
} else {
    return argument2
}
}
alert(max(39,68));