JavaScript JavaScript Basics 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

Brandon Van Camp
Brandon Van Camp
Front End Web Development Techdegree Graduate 28,695 Points

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

Omar Ocampo
Omar Ocampo
1,820 Points

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

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

Omar Ocampo
Omar Ocampo
1,820 Points

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

Omar Ocampo
Omar Ocampo
1,820 Points

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?