Back to the lesson

Function min(a, b)

importance: 1

Write a function min(a,b) which returns the least of two numbers a and b.

For instance:

min(2, 5) == 2
min(3, -1) == -1
min(1, 1) == 1

A solution using if:

function min(a, b) {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}

A solution with a question mark operator '?':

function min(a, b) {
  return a < b ? a : b;
}

P.S. In the case of an equality a == b it does not matter what to return.