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

Mohammed Jammeh
Mohammed Jammeh
37,463 Points

Functions - Using numbers as parameters

Hi, I am struggling to solve a question under JavaScript basics - functions.

The question reads "Create a new function named max() which accepts two numbers as arguments. The function should return the larger of the two numbers. You'll need to use a conditional statement to test the 2 numbers to see which is the larger of the two".

The code below is my answer but it keeps saying there is parse error within the code which I believe is related to the numbers I'm using. I will really appreciate it if you could help. Thanks!

function max (10,15) {
  if (15>10) {
    return true;
  } else {
    return false;
  }
}

4 Answers

Hi Mohammed!

Your code is almost perfect! Instead of putting numbers straight into the function you have to put some variable names which can then be used to refer to the number you pass to the function.

function max(num1, num2) {
    if(num1>num2) {
         return true;
    } else {
        return false;
    }
}
Mohammed Jammeh
Mohammed Jammeh
37,463 Points

Thanks Luke.

With the help of your answer, I have solved it. I just remembered variables do not begin with numbers.

For some reason, the Boolean values were not working either. I had to put the parameters beside 'return' instead of true/false. For example, instead of:

function max(num1, num2) {
    if(num1>num2) {
         return true;
    } else {
        return false;
    }
}

I had to put:

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

By the way, thanks man :)

I am glad you managed to figure it out! Have fun learning JavaScript!

Hope you are having a good day Mohammed.

-Luke

Mohammed Jammeh
Mohammed Jammeh
37,463 Points

Thanks Luke, you too.

I have managed to answer the first question of the challenge but I'm now stuck on the second one ha ha.

Would you like some assistance with that question too?

Mohammed Jammeh
Mohammed Jammeh
37,463 Points

Yes please:

Question: Call the function and display the results in an alert dialog. You can pass the results of a function directly to the alert() method.

My solution:

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

alert( max(num1,num2) );

.. but it's not passing through.

What you need to do in this case Mohammed is what you were doing for task 1. Try passing in some integer values and see what happens!

I hope this helps.

-Luke

Mohammed Jammeh
Mohammed Jammeh
37,463 Points

It worked, thanks a lot man.

If you don't mind, what's the difference between 'num1' and the integer '1'?

I'm glad you got it working.

When you are declaring a function you can give it some arguments (variables) that it can use within the function. These can be named anything like num1, word, thisIsASentence and etc.

Whereas 1 or 50 is something that you would pass to the function so it can use it.

function max(num1, num,2) {

}

max(50, 25)

In the case above num1 is 50 and num2 is 25. I hope this helped clear up your confusion a bit.

-Luke

Mohammed Jammeh
Mohammed Jammeh
37,463 Points

It surely did. Thank you man; I really appreciate it.

No problem! Have fun learning JavaScript!

Chanse Campbell
Chanse Campbell
4,453 Points

+1 Thanks for this guys, I was having major issues with this challenge for some reason but this really cleared it up for me :)

Good job Chanse! Keep up the great work!

A FRENZ
A FRENZ
2,207 Points

I think there asking to return a number here:

var num1 = 2; var num2 = 3;

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