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

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

[HELP] Challenge Task 1 of 2 javascript parameter

here is the task 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.

the hint say u need to use conditional statement. but i just type this code then i pass without typing conditional statement.

is my code right?? or are there any complete code to boost my comprehension!!

Thanks :)

script.js
function max(num1, num2) {

  return num1, num2
}

8 Answers

Hi Rifqi,

Not quite. A conditional statement is one that checks to see if a statement is true or false (a boolean value). So you'd want to do something like this:

function max (a, b) {

    if (a>b) {
      return a;
    } else {
      return b;
    }
}

This says that if a is greater than b, return a; otherwise return b. (Please note that it does not take into account numbers that are of equal value. Well, technically I suppose it would return b if that were the case, but that's likely to be a suboptimal result. :))

Best, Cena

(you can name the arguments, whatever you would like)

I understand everything but one thing in your explanation: where are the arguments? Shouldn't you call them at the end?

Jack O'Brien
Jack O'Brien
2,941 Points

Anybody else cruise through HTML and CSS so far and then encounter JS be like wtf lol. It is almost a new dialect, vastly more complex than HTML or CSS

Aron Jeremic
Aron Jeremic
2,798 Points

I tried this code and works nicely, I passed the task.

function max(x, y) {
  var bigger = Math.max(x, y);
  return bigger;
}

I'm sorry, I'm stuck in this task, Does anybody have the correct answer?, please let me know

Thanks a lot guys

David Durant
David Durant
4,387 Points

I think it is because you passed it numbers. When you make the Function think that the arguments() are like naming Variables can't start with a number. So, if you set (3,6) at the top the function will fail... but if you write it this way:

function max(x,y){ if( x > y){ return x; }else{ return y; } } max(6,3);

you are basically saying var x = 6 var y = 3

and the function max will run.

Brandon Perkins
Brandon Perkins
1,499 Points

Exact issue I was having. Super helpful. Thanks David!

This code works perfect with letters but not with numbers, thanks

Hi Jon,

It's because you're attempting to pass arguments into your function definition, rather than parameters (which are placeholders for actual values, or arguments.)

For the above to work, it would need to look like this:

function max (a, b) {
   if (a > b) {
      return a;
   } else {
      return b;
}
}

then call the function with:

max(3,6);
jonschafer
jonschafer
2,229 Points

Ahhhhh! Thank you so much!

function max(x,y){ if ( x > y){
return x; } else {
return y; } } //this works

Gertrude Dawson
Gertrude Dawson
5,371 Points

Thank you all. I now understand the theory behind the answers.

jonschafer
jonschafer
2,229 Points

So, why doesn't this work?

function max (3, 6) {
  if (6 > 3) {
    return 6;
  } else {
    return 3;
  }
}

you got it , just change your 3 & 6. so function max (6,3) { if (6 > 3) { return 6; } else { return 3; } } (the problem is your original placement of (3,6) switch it to (6,3) and it works! ) Think of it as a & b , If a is greater than be then so on. Remember the original placement is (a, b) .