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

Challenge Task 1 of 2

i stucked here

3 Answers

Steven Parker
Steven Parker
231,096 Points

I'll try being a bit more explicit, and go line-by-line.

function max(tea, milk){  // good function definition, and your two parameters are "tea" and "milk"
    var tea = 6;          // but don't create a new variable with the same name as a parameter
    var milk = 5;         // same here, neither of these will be needed
    if (6 > 5) {          // use the parameters to represent values, not actual numbers
        return 6;         // return a paramter not an actual number
    }
                          // so if the test failed, return something else here
}

If the hints still don't help, you might find it worthwhile to review the Giving Information to Functions video.

Steven Parker
Steven Parker
231,096 Points

Personally, I wouldn't want to see a "give solution" button.

I think working to resolve issues helps much more to learn that just seeing answers. If you want to see example code, just review the video. And you never need to "wait for a solution", you can always skip a challenge or quiz and continue to the next video.

Now for your challenge code I'll give you some hints:

  • you should never create new variables with the same names as passed-in parameters
  • use the parameters to represent values, don't hard-code values in your function
  • when you return something inside an if code block, you also need to return something else after it

well, I just got it now Thanks, Steven!

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

 alert(max(6, 5));
Steven Parker
Steven Parker
231,096 Points

Excellent! :+1: Persistence pays off!

Happy coding!