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

Create a new function named max() which accepts two numbers as arguments....? what am i suppose to do?

Create a new function named max() which accepts two numbers as arguments. The function should return the larger of the two numbers. You'll need to use a conditional statement to test the 2 numbers to see which is the larger of the two.

function max(2, 3) { return number2 var number2 = 3; }

i just cant seem to figure this out... please help

script.js
function max(2, 3) {
 return number2 
var number2  = 3;
}

1 Answer

Blake Lieberman
Blake Lieberman
23,772 Points

Notice I use num1, num2.

These represent the input variables of two numbers.

I use a if statement to determine if the 1st number(num1) is greater than the 2nd(num2). If its true I return the first number(num1).

Now if it doesn't match that statement one of two possibilities exist either they are equal and it doesn't matter which one i return as they are both the max number. Or num2 is greater than num1 and I return num1.

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

max(2,3); //expected answer is 3

Hope this helps. If you need more help just ping me or request I answer your forum questions in the future.

for max(2, 3); why dont you put document.print, alert, or something along those line to call for the execution?

and i still dont get functions. -_-

side note: Your Score....the specificity of this answer! PLEASE tell do you make a living programming because im in college and i want to web design as my occupation. WHAT DOES IT TAKE, HOW LONG, HOW HARD???

Blake Lieberman
Blake Lieberman
23,772 Points

Yes, treehouse will also help you find jobs after you complete a path. There will be and are plenty of tech jobs. Functions are like verbs. They are actions that your program can perform. Now how do you create an equation to solve hypothetical problems. Sounds like algebra x + y = z. x,y,z are variables, just like num1, num2 where variables for this particular function.

It takes passion. Don't do this unless you can't sleep and want to learn more. Write code! Examples, have goals, my goal was to be able to make a facebook login page. Now its easier than cake. Go through the Front-End Developer Course. When you have problems just request me to help you answer them.

Can you award me the answer? It is required for a path I am taking.

if you mean hit the best answer button, absolutely. before i do one more thing, why is it required and if you wouldn't mind could you describe to me the path you are taking (im sure its obvious, buy just making sure)? how long have you been doing this, and what your work ethic?

p.s. im just very curious and like to learn from others.