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

JavaScript function challenge

I feel a little dumb... Can someone tell me what I am doing wrong here? More importantly, can someone also tell me WHY it's wrong? I've been going over this challenge for a bit now and I'm just having a hard time getting the hang of functions.

Thank you.

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

2 Answers

Merritt Lawrenson
Merritt Lawrenson
13,477 Points

You're almost right. your function takes in two arguements - that is, num1 and num2 are the two numbers you give it. Your if/else statement is also formatted correctly. Where you made a mistake is in the return.

If number one is bigger, your function should return that number. If number two is bigger, your function should return number 2 instead. The return is what the function spits back out when it's done. In this case, you want your function to return the bigger number. Right now, the return value is the function itself, which doesn't make sense. You want the return value to be the bigger number. If num1>num2, you want to return num1. Otherwise, you want to return num2.

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

Aha! Thanks guys. I really appreciate the help here. One of those dumb things that I just wasn't seeing clearly. :)