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

Shane Wax
Shane Wax
4,332 Points

Call the function and display the results in an alert dialog. You can pass the results of a function directly to the ale

im a bit confused, please help

script.js
function max( num5, num8) {
  if ( parseInt(num5) < parseInt(num8) ) {
    return num8;
  }
}

alert( max.parseInt(num8);

1 Answer

Felix Sonnenholzer
Felix Sonnenholzer
14,654 Points

Hi Shane,

to call a function you need the following syntax:

functionname(parameter1, parameter2);

The parameters are only a reference to variables inside your function.

I suppose you want the function to compare the number 5 and 8?

In this case your code should look like this:

function maxNumber( number1, number2 ) {
    if ( number1 === number2 ) {
        return "The numbers are the same";
    }
    if ( number1 < number2 ) {
      return number2;
    } else {
      return number1;
   } 
}

alert( maxNumber(5, 8) );

You don't need the parseInt method if you are careful to only call the function with numbers. The first if statement is to check the event, when both numbers are equal. You can call the function with any numbers you want, remember that the 'number1' and 'number2' are only a reference.