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

Syntax error

How do solve this one!

script.js
function max (3, 6) {

    if (3>6) {
      return 3;
    } else {
      return 6;
    }
}
max(3,6);
Adam McGrade
Adam McGrade
26,333 Points

The main purpose of creating a function is so that you can reuse the same code to perform a task many times. Your code is functionally correct, however it will not allow you to reuse the code, as your are providing values (3,6) in the declaration of the function as arguments. When declaring a function you want to use placeholder values so that the function can be reused with many numbers. When calling the function, you pass the values you wish to test into the method as arguments.

function max(number1, number2) {
   if (number1 > number2) {
     return number1;
   } else {
     return number2;  
   }
}

max(3,6) // 6

Now you are able to pass any two numbers, rather than 3,6 and it will perform the comparison for you.

1 Answer

Steven Parker
Steven Parker
231,275 Points

When you create a function, you don't use actual values as the parameters. Instead you use names which act as "placeholders" and represent the values that will be passed to the function when it is called in the program.

Just replace your numbers with names and you should pass task 1.

Then for task 2, when you call the function (and here the numbers are OK), just make your call be the argument to "alert()".