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

Nassor Khalfani
Nassor Khalfani
5,058 Points

return max

whats the syntax to return a the max number again

script.js
function max (411, 818)
{

  return 411;
}

3 Answers

The syntax for your particular data set would be as follows

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

max(411, 818);

If you would like the largest number in an array of numbers, the syntax would be

let numbers = [1, 6, 3, 45, 9, 11, 345, 78, 19];

function max(){
    let maxNum = numbers[0];
    for(num in numbers){
        if(numbers[num] > maxNum){
            maxNum = numbers[num];
        }
    } return maxNum;
}

max();

well first of all you should not put a numbers as arguments you should put a refrence like a and b then if the function u will need to use if statement

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

You need to set up a conditional statement to return the larger number. Hint: use a comparison operator in the if condition.

function max ( first number, second number) {
    if (condition) {
   return 
} else {
 return
}
}

max()