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

Chantal St Louis
Chantal St Louis
5,524 Points

I don't understand how my code is incorrect. How can I correct this please?

I've created the 'Max' function, given it the two arguments 'smallest' and 'biggest'. I've tested my two variables and that test should have it return the larger number of the two. i don't understand how this wrong

script.js
function max(biggest, smallest){
    var biggest = 55;
  var smallest = 5;

if (biggest === 12 || smallest === 5){
  return smallest;
} else {
  return biggest;
  }
}
Chantal St Louis
Chantal St Louis
5,524 Points

I realised I failed to call the function after... I've since called the function at the end, by adding max(); it stills says my answer is incorrect.

1 Answer

Kevin Gates
Kevin Gates
15,053 Points

Hi there,

Your if statement is only testing to see if the biggest number is equal to twelve OR if your smallest is equal to 5.

You need to have a couple things. Your max function should take two arguments and return the highest (max).

So let's figure out what we need to do:

  1. Provide two numbers.
  2. Compare the Numbers
  3. Return the highest.
  4. Assign that function (1-3) to a named function.

So a standard function is created like this:

function max() {
  // Your code.
}

You need to provide two numbers,

function max( num1, num2 ) {

}

Then you need to compare the two. You can do this with an if statement or a tertiary expression. I'm using the later:

function max(num1, num2) {
  (num1 > num2 ) ? num1 : num2;  // I'm testing if num1 is greater than number 2, if yes, I return num1, else num2. 
}

Finally, I'm returning the number when the function is called:

function max(num1, num2) {
  return (num1 > num2 ) ? num1 : num2;  // I'm testing if num1 is greater than number 2, if yes, I return num1, else num2. 
}
Chantal St Louis
Chantal St Louis
5,524 Points

Thank you so much Kevin, this was just what I needed. I hesitated in asking for help because I just "knew" I wouldn't understand the explanations I'd get in response. You made this super easy to understand thanks again :o)

Kevin Gates
Kevin Gates
15,053 Points

Hi Chantal St Louis , that's great to hear! I'm glad I was able to clearly explain it for you. Happy coding, and always be willing to ask questions. It's how we learn :)