Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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;
}