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

this doesn't work? function max( num1, num2 ){ } if ( num1 > num2 ){ return num1; } else { return num2; }

i'm stuck on the challenge task 1 of 2 asking to create a new function and returning the larger number using conditional statements. thanks!

script.js
function max( num1, num2 ){

}

if ( num1 > num2 ){
  return num1;
} else {
  return num2;
}

2 Answers

Samuel Webb
Samuel Webb
25,370 Points

Jeffrey,

The problem is that you're ending the function before the conditional statement. The if statement should be inside of the max function, not after it. Your code should look like this:

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

Hope this helps.

Cheers, Sam

Thanks Samuel!

Samuel Webb
Samuel Webb
25,370 Points

No problem. Glad I could help.