GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

No menu could be recovered. Follow the blue links in the page.

This view

  • Original page
  • New address

GeezerVison is showing javascript.info — Function min(a, b)

  • Tutorial
  • The JavaScript language
  • JavaScript Fundamentals
  • Functions
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.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.