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 Functions Pass Information Into Functions Create a max() Function

jeff funnell
jeff funnell
4,452 Points

please give me the answer to this task, I have tried lots of ways, but am very limited based on the limited training TIA

script.js
function max(num1, num2) {
if ( numb1 > numb2 ) {
   return numb1;
  } else {
    return numb2;
  }

}

2 Answers

jeff funnell
jeff funnell
4,452 Points

All good, I got through it

I am assuming you realized that the issue is that you are using the variable names numb1 and numb2 inside the function, but your parameter names are actually num1 and num2 (without the b).

Something else that I wanted to mention that you may not have learned or realized yet, but you don't have to use the else statement in this situation. The return statement exits the function, so nothing after it is called. What you have is not wrong (other than the variable names), but it can make your code a little more concise if you do it the other way. Also, if you only have a single statement as the code that should run if the conditional is true, you can omit the curly braces. Lastly, as a matter of form, you should indent statements within your function just like you have within your conditional, and you should also not have the blank line before the closing curly brace for the function. These are not rules, but they are syntax standards that will make your life easier when reading through code, whether yours or someone else's. I know that in this tiny example it doesn't really matter, but when you're looking through hundreds (or thousands) of lines of code, it really makes a big difference, and you should develop this habit early. This is how you might see it look if another developer wrote it:

function max(num1, num2) {
    if ( num1 > num2 ) return num1;
    return num2;
}