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

Can you please assist with the question below: Create a new function named max which accepts two numbers as arguments (y

Create a new function named max which accepts two numbers as arguments (you can name the arguments, whatever you would l

script.js
var 


function max(arg1, arg2) {
  return large;
}
if 

4 Answers

Hey Josephine,

function max(num1, num2) { // num1 && num2 are our arguments 
            // Next we need to figure out what number is greater, num1 or num2. In this case we are passing 12 = num1 and 3 = num2..
    if(num1 > num2) {  // So if num1 is greater, which it is in this case.
    return num1; // We then return num1
  } else {
    return num2;
  }
}
console.log(max(12, 3));

Another case if num2 happened to be our greater number.

function max(num1, num2) { // num1 && num2 are our arguments 
            // Next we need to figure out what number is greater, num1 or num2. In this case we are passing 1 = num1 and 31 = num2..
    if(num1 > num2) {  // So if num1 is greater, which now it's NOT && ='s( equals ) false.
    return num1; 
  } else { 
    return num2; // We then return num2
  }
}
console.log(max(1, 31));

Hopefully this helps you out. Let me know if you need further help, thanks.

Thank you soo much.. It has really helped

I'm sorry, it didn't work for me

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

function max(10, 50) { if (10 > 50) {
return 10; } else { return 50; } }

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

I'm sorry, I'm doing accordin this example; but it does not work for me, Could assist me you this please

Days ago posting request to my problem and no answers, please, anyone can help?

function max(a,b) { if (a > b) { return("first is greater"); } else { return("second is greater"); }

max(4,5);

its saying the 'max' function isn't returning a number. How do i correct that error?