With Lars Berg filtered out, the timing desk’s next figure is the average finish time, which needs each finish string, like "1:42:37", as seconds. The Python helper hands int to map() by name. That habit ports almost unchanged, and the “almost” costs Ana Ruiz her finish time.
Port the Converter by Name
Here’s the Python helper, which gives 6157 for Ana’s finish:
def to_seconds(time):
h, m, s = map(int, time.split(":"))
return h * 3600 + m * 60 + s
Functions are values in JavaScript too, so the natural port passes parseInt() by name and unpacks the result with array destructuring. Predict what it logs:
const toSeconds = (time) => {
const [h, m, s] = time.split(":").map(parseInt);
return h * 3600 + m * 60 + s;
};
console.log(toSeconds("1:42:37"));
console.log("1:42:37".split(":").map(parseInt));
There’s no error, but only the hours survive:
NaN
[ 1, NaN, NaN ]
Python’s map() calls int with one argument. JavaScript’s .map() always calls its callback, the function you pass, with three: the item, its index, and the whole array. .forEach() calls its callback the same way, so you can use it to show all three arguments:
"1:42:37".split(":").forEach((item, index, array) => {
console.log(item, index, array);
});
Each call gets the index and the array along with the piece:
1 0 [ '1', '42', '37' ]
42 1 [ '1', '42', '37' ]
37 2 [ '1', '42', '37' ]
Where Python raises a TypeError for extra arguments, JavaScript lets a function ignore them. But a function with a second parameter gets the index, and the second parameter of parseInt() is the radix, like the base of Python’s int(). The first call is parseInt("1", 0), where a radix of 0 counts as no radix at all. The hours therefore come through in base 10. Then parseInt("42", 1) and parseInt("37", 2) give NaN.
Pass Only What You Mean
To fix it, pass a function that uses only the item. Number() ignores extra arguments, and an arrow can pass parseInt() the item and the radix:
console.log("1:42:37".split(":").map(Number));
console.log("1:42:37".split(":").map((s) => parseInt(s, 10)));
Both produce the same numbers as Python’s map(int, ...):
[ 1, 42, 37 ]
[ 1, 42, 37 ]
That holds because the desk’s times are well formed. Neither conversion checks its input the way int() does. Number() reads "4.5" as 4.5 and turns "" into 0, and parseInt() reads only the leading digits, so "42junk" gives 42. Where int() raises, they give a number or NaN instead.
Here’s how the Python habits map across:
| Python | JavaScript |
|---|---|
map(int, parts) |
parts.map(Number) |
map(str.strip, cells) |
cells.map((c) => c.trim()) |
for t in times: print(t) |
times.forEach((t) => console.log(t)) |
Pass a function by name only when you know it ignores an index and an array. When in doubt, write the arrow. Python’s str.strip works by name because it takes the string as its first argument. JavaScript’s .trim() takes no argument and works on the string it’s called on, which .map() never supplies, so it needs the arrow too. That’s all this lesson needs about methods.
Choose Between map() and forEach()
With toSeconds() fixed, the desk wants an array of seconds for the average and a printout to check against the timing sheet, both using the six finish times in finisher order:
const toSeconds = (time) => {
const [h, m, s] = time.split(":").map(Number);
return h * 3600 + m * 60 + s;
};
const finishTimes = [
"1:42:37", "1:30:48", "1:36:20",
"1:27:44", "1:35:03", "1:41:15",
];
const seconds = finishTimes.map(toSeconds);
console.log(seconds);
const logged = finishTimes.forEach((t) => {
console.log(`${t} is ${toSeconds(t)} s`);
});
console.log(logged);
.map() builds the new array, and .forEach() only logs, returning undefined:
[ 6157, 5448, 5780, 5264, 5703, 6075 ]
1:42:37 is 6157 s
1:30:48 is 5448 s
1:36:20 is 5780 s
1:27:44 is 5264 s
1:35:03 is 5703 s
1:41:15 is 6075 s
undefined
You can pass toSeconds by name safely because it uses only its first argument. Its body has braces, so it keeps its return. Without it, .map(toSeconds) would give six undefined values.
.map() builds its new array at once, unlike Python’s lazy map(). Use .forEach() for a side effect such as logging, as you’d use a Python for loop that only calls print(). A for...of loop is equally idiomatic. Save .map() for when you use its result.
Before the average, the Python script builds a dict of finish times by bib, the set of age groups, and a list of dicts for the results page. In the next lesson, Building Objects and Sets From Arrays, you’ll port those comprehensions the JavaScript way: build an array, then turn it into what you need.