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

I am stuck here, anyone could help me? Thanks!

I have been trying differents options but I don't find the right answer... I am made a mess :(

script.js
function max(upper){

}
Simon Coates
Simon Coates
28,694 Points

Can you post an attempt? Or could you confirm that you want the full answer? In terms of a learning experience, it might be more beneficial to post an attempt, and get feedback. FYI, Nicholas Vogel is right about the method signature. You just need to use an if statement and two return statements. Don't need an else, as anything outside an if that includes a return statement is implicitly an else.

5 Answers

Nicholas Vogel
Nicholas Vogel
12,318 Points

The challenge says you need two parameters, an upper and a lower.

function max(upper, lower) {

}

Thank you I got it!

function max (upper, lower){ var lower; var upper; if(lower < upper){ return upper; } else { return lower; } }

max()

I did it :D

Simon Coates
Simon Coates
28,694 Points

my solution was

function max(first, second) {
    if(first>second) {
      return first;
    }
    return second;  
}
alert(max(4, 3));

I;m not sure about your declaring variables. I don't know what it does. initially, i thought declaring your variable might interfere with your parameters. But it seems to work. Hence i'm curious if it alters anything (?).

function max (upper, lower){ var lower = 1; var upper = 100; if(lower < upper){ return upper; } else { return lower; } }

I have been using this code in the console of chrome. You can change the numbers of the variables for know which number is greater than the other. For this reason I have used variables.

Do you understand?

Simon Coates
Simon Coates
28,694 Points

but parameters are already variables. You could overwrite the parameter values without using the var keyword:

function max(first, second) {
    first = 45;
    second = 89;
    if(first>second) {
      return first;
    }
    return second;  
}
alert(max(4, 3));

True! I did not know it! Great, Thank you! :)