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

Joshua Phillips
Joshua Phillips
3,429 Points

function

This is the question that I need to solve. I am having trouble solving this. Please show me how you would solve it and I can use that for my notes to remember.

Create a new function named max which accepts two numbers as arguments (you can name the arguments, whatever you would like). The function should return the larger of the two numbers.

HINT: You'll need to use a conditional statement to test the 2 parameters to see which is the larger of the two.

4 Answers

Steven Parker
Steven Parker
231,109 Points

It doesn't look like you've written any code yet. Give it your best try, and if you're still stuck, share your code so far and someone can help you with it.

If you really have no idea how to start, you may want to review the video about functions or the ones about giving and getting information from functions.

Joshua Phillips
Joshua Phillips
3,429 Points

I have reviewed the videos and trying to piece it all together I'm not getting it.

Joshua Phillips
Joshua Phillips
3,429 Points

This is my code that I'm stuck on

function max(red, blue) { var red = 20; var blue = 30; return answer; }if (blue > red){ answer = blue; }

Thomas Nilsen
Thomas Nilsen
14,957 Points

This was your code:

function max(red, blue) { 
    var red = 20; 
    var blue = 30; 

    return answer; 
}

if (blue > red) { 
    answer = blue; 
}

The answer is easier than you think.

You're supposed to make a function (just make it, not call it) that accepts to arguments. If the first argument is greater than the other, you return that. Otherwise you return the second argument.

That would look like this:

function max(red, blue) { 
    if (red > blue) {
        return red;
    } else {
        return blue;
    }
}