Seven Segment Display - Riddle by FiveThirtyEight - Solved with JavaScript

The FiveThirtyEight riddle Tyler Barron’s Seven Segment Display is stated as follows.

Given a two-character, seven-segment display, like you might find on a microwave clock, how many numbers can you make that are not ambiguous if the display happens to be upside down?

For example, the number 81 on that display would not fit this criterion — if the display were upside down it’d appear like 18. The number 71, however, would be OK. It’d appear something like 1L — not a number.

// Rotates a single seven-segment display digit 180 degrees.
// Non number results are converted to NaN.. eg. 2 --> 2, 6 --> 9, 3 --> NaN
const rotateDigit = n => [0, 1, 2, NaN, NaN, 5, 9, NaN, 8, 6][n];

// Rotates a seven-segment display number 180 degrees.
// eg. 10 --> 01, 81 --> 18, 71 --> 1NaN.
const rotateNumber = n =>
    // Convert the number to a string.
    n.toString()
    // Split the string on every character.
    .split('')
    // Reverse the order of the characters
    .reverse()
    // Rotate the digits.
    .map(rotateDigit)
    // Convert the array back to a string.
    .join('');


const numbers =
    // Generate a range from 0 to 99.
    [...Array(100).keys()]
    // Rotate every number.
    .map(rotateNumber)
    // Filter every number that is not a number. :)
    .filter(isNaN)

const result = numbers.length;

Keywords