Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Finding the Fastest With Math.min()

Finding the Fastest With Math.min()

The live results page names the fastest finish time and the runner who ran it. The Python script finds both with min(): min(seconds) returns the smallest time, 5264 seconds, and min(finishers, key=lambda r: to_seconds(r["finish"])) compares the finishers by their times and returns the runner, Priya Nair.

  • JavaScript keeps its minimum and maximum on the Math object. Ported as Math.min(seconds), the line prints Fastest finish: NaN, with no error and no warning.
  • Math.min() compares the numbers you pass as separate arguments: Math.min(6157, 5448) is 5448. It turns each argument into a number, and an array of six numbers isn’t one: Number(seconds) is NaN, and any NaN argument makes the result NaN. Python’s min() accepts one list or several values; Math.min() has only the second form.
  • Spread the array into the call, Math.min(...seconds), like Python’s star in print("Missing chip reads:", *missing), so each time becomes its own argument. It returns 5264, as Python does, and Math.max(...seconds) returns 6157, Ana’s time, the slowest finish.
  • At the start gun, seconds is empty. Python’s min() raises ValueError: min() iterable argument is empty there, unless you pass default=. Math.min() with no numbers returns Infinity, and Math.max() returns -Infinity, so check the array’s .length first.
  • Math.min() has no key argument. For the runner, reduce the finishers to the best so far: the function takes the best runner and the next, and the conditional operator keeps the next runner only if their time is smaller.
  • With no start value, reduce() takes the first runner as the best and calls the function five times for six runners: Tom replaces Ana, Priya replaces Tom, and Priya stays. Here the first runner is the right start, because the best so far has to be a real runner.
  • Like min() with no default=, this reduce() throws on an empty array (TypeError: Reduce of empty array with no initial value), so check the length first when that can happen. Like the desk’s Python, which has no default= yet, this port still stops at the start gun, after it logs Infinity. With <, a tie keeps the earlier runner, as min() does.
  • When the numbers come from records, map() them first, then spread that. A very long array, hundreds of thousands of numbers, can be too many arguments for one call, so reduce it instead.

Here’s the fastest finish and the fastest runner, with the records trimmed to the two fields this code reads, and the slowest finish logged as well:

Language: JavaScript
const toSeconds = (time) => {
  const [h, m, s] = time.split(":").map(Number);
  return h * 3600 + m * 60 + s;
};

const runners = [
  { name: "Ana Ruiz", finish: "1:42:37" },
  { name: "Tom Okafor", finish: "1:30:48" },
  { name: "Mei Chen", finish: "1:36:20" },
  { name: "Lars Berg", finish: "DNF" },
  { name: "Priya Nair", finish: "1:27:44" },
  { name: "Sam Kowalski", finish: "1:35:03" },
  { name: "Jo Lindqvist", finish: "1:41:15" },
];
const finishers = runners.filter((r) => r.finish !== "DNF");
const seconds = finishers.map((r) => toSeconds(r.finish));

const fastest = Math.min(...seconds);
console.log("Fastest finish:", fastest);
console.log("Slowest finish:", Math.max(...seconds));

const winner = finishers.reduce((best, r) =>
  toSeconds(r.finish) < toSeconds(best.finish) ? r : best
);
console.log("Fastest runner:", winner.name);

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The live results page names the fastest finish time, and the runner who ran it. Python finds both with min(), one line each. Given the list of finish times in seconds, min() returns the smallest.

00:15 Given the finishers and a key function, it compares them by their times, and returns the runner. Python prints 5264 seconds. That’s Priya Nair’s time, and she’s the fastest runner.

00:30 JavaScript has a Math.min(), so the port looks like a one-word change.

00:36 The seconds line is already ported, a map() over the finishers. In JavaScript, Math.min() and Math.max() are the minimum and maximum functions, kept on the Math object.

00:47 So in the port, min() of seconds becomes Math.min() of seconds, and the print line becomes a console.log() call.

00:56 Python printed 5264. What does the JavaScript print? Your turn: pause the video and predict.

01:10 It prints NaN, not a number. There’s no error and no warning, so that NaN would go straight onto the live page.

01:21 Math.min() compares the numbers you pass as separate arguments: 6157, 5448, and so on. The port passes one argument, the whole array. Math.min() turns each argument into a number, and an array of six numbers isn’t one.

01:42 Number() of seconds gives NaN, and any NaN makes the result NaN. Python’s min() accepts both forms. Give it one list, and it compares its items.

01:54 Give it several values, and it compares those. Math.min() has only the second form.

02:02 So spread the array into the call, with three dots, like Python’s star when you printed the missing bibs. Each time becomes its own argument. Run it again, and JavaScript prints 5264, the same as Python.

02:19 Math.max() works the same way, and gives 6157, Ana’s time, the slowest finish. At the start gun, seconds is empty. Python’s min() raises a value error there, unless you pass a default.

02:37 Math.min() with no numbers returns Infinity, and Math.max(), minus Infinity. So where Python passes a default, check the array’s length first.

02:49 For the runner, Python passes min() a key function. Math.min() has no key argument, and it returns a number, never a runner. Instead, reduce the finishers to one runner: the best so far.

03:05 The function takes the best runner so far and the next runner. The conditional operator picks one. If the next runner’s time is smaller, it’s that runner, otherwise the best.

03:17 Run it, and JavaScript prints Priya Nair, as Python does.

03:23 There’s no start value this time. So reduce() takes the first runner, Ana, as the best so far. Tom’s time is smaller, so Tom is kept. Mei’s is slower.

03:37 Priya’s beats Tom’s. Sam and Jo don’t beat hers: five calls, and Priya stays. For totals, you always pass a start value. Here the first runner is the right start: the best so far has to be a real runner.

03:54 Like min() with no default, this reduce() throws on an empty array, so check the length first when that can happen. With less-than, a tie keeps the earlier runner, as min() does.

04:09 Spread the array into Math.min() or Math.max() for the smallest or largest number. For numbers in records, map them first, as the seconds line does, then spread that.

04:20 For the item itself, as with a key function, reduce and keep the better one. Check for an empty array first, where Python passes a default. A very long array, hundreds of thousands of numbers, can be too many arguments for one call, so reduce that too.

04:39 Next up is Answer Questions at the Results Desk, an exercise. There, you’ll answer the desk’s questions, from dropouts to the fastest and slowest finish, with array methods.

Become a Member to join the conversation.