JavaScript JavaScript Functions Pass Information Into Functions Create a max() Function

FRANKIE Ochoa
FRANKIE Ochoa
1,944 Points

can someone please help I don't understand

script.js
function max (3, 6) {
  if (6 > 3) {
    return 6;
  } else {
    return 3;
  }
console.log( addNumbers(3,6) );

2 Answers

Peter Vann
Peter Vann
26,472 Points

The objective is: Create a function named max that accepts two numbers as arguments (name the parameters whatever you would like). The function should return the larger of the two numbers.

(Hint: pass the parameters in the function as variables, as in 'a' & 'b' or 'x' & 'y' or 'param1' & 'param2')

So my solution would be

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

console.log( max(4, 7) );

Will log 7 to the console.

Peter Vann
Peter Vann
26,472 Points

Bonus:

I prefer declaring functions like this, however:

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

which can also be shortened to this using ES6 arrow function syntax:

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

More info on ES6: https://codeutopia.net/blog/2015/01/06/es6-what-are-the-benefits-of-the-new-features-in-practice/

FRANKIE Ochoa
FRANKIE Ochoa
1,944 Points

thank you that helps alot