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

How do return larger number of the two parameters I set

Am not understanding this quiz .any recommendations plz

script.js
function max(23,45)
return 23,45;

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi there Abdullahi,

As I understand it, the code challenge is asking you to use an if statement to get the largest value.

I'd start by first of all passing in 2 variables as parameters to the max function. for example, max1 and max2 but they could be anything.

function max(max1, max2) {

}

Remember these are just variables that will contain the values you'll pass in as arguments.

You can use a condition statement like so.

if(max1 > max2) {
  return max1;
} else {
  return max2;
}

So what's happening here is when you return max1 you're assuming that the value for max1 will be bigger than max2. JavaScript will then execute that condition blog.

You could add an elseif condition to test for equal values but I don't think you need to worry about that for the challenge. The elseblock may suffice. If max1 isn't the largest number, max2 will be.

The way you determine this is to call the function with the actual values, like so.

max(3,2);

This will make sure the if block returns max1 as the largest number. Hope this helps.

I recommend that you rewatch the sections on conditionals, and functions. Your function will cause an error, as well as your return statement. Consider what it is you would like to do: if one number is larger you'd like to return it; if the other number is larger then you'd like to return it.