Mark Grealish

the life and code of a crazy cat man


in code

Hackoberfest has made Tic-tac-toe a thing in my life. tictactoe-server opened an issue for a ES6 refactor of the solution checker.

I kicked around the lengthy imperative code check and found I wanted to expand the solution, that I wanted to drop in Tic-tac-toe boards of arbitrary size, and to check them in an efficient manner.

My solution, when given a board and a target character:

  1. Do all the cells of one row contain only the character?
  2. Do all the cells of one column (transposed to a row) contain only the character?
  3. Do all the cells of one diagonal (forward and backwards, transposed to rows) contain only the character?

I transpose the rows because iteration over the columns seems tedious-more code, same end. Rather than write three checks each run once, I fit the code to one check run once.

Check Solution

Let’s dive in!

function check(character, board) {
    return [].concat(board, transpose(board), diagonals(board)).some(row => {
        return row.every(cell => cell === character);

Given the board and the target character, use some() to every() check the conditions given above. Does any row have cells containing only the character?

There are two sub-methods called:

Transpose Array

function transpose(array) {
    return array[0].map((_, index) => => row[index]));


  1. Take the first element, an array.
  2. For each element in that array, map the index of each item in the parent array to that position.

Get Array Diagonals

function diagonals(array) {
    const reversed = array.slice().reverse();

    return [, index) => array[index][index]),, index) => reversed[index][index])

To explain:

  1. Walk the multidimensional array in a forward direction to get diagonal element.
  2. Reverse the array and repeat step 1.

Grab it on node and try it today!

Moody me

in me

Taken by Eadaoin in Brussels, Belgium.

Moody Mark

We’re on a plane!

in family

To Brussels!

Eadaoin and I on a plane!


in random

Reddit jokes aside, holy shit: I wooed Eadaoin with space facts. We would spent hours on Skype at night when we first went out, where I’d talk about the universe to help get her to sleep.

Making Friends

Decadent kitty comfort

in animals

You can’t fake this level of luxury.

Sleeping Cookie

Get the prime factors of a given number

in code

This solution uses the Sieve of Eratosthenes and the range() functions I detailed a while back. Hurray for function composition!

function factors(number) {
    const primes = sieve(range(2, number >> 1)).filter(n => !(number % n));
    return primes.length ? primes : [number];

The code is simple:

  1. Generate a list of primes in range 2-(n / 2) (inclusive).
  2. Filter the list to remove numbers that aren’t multiples of n.
  3. If the list is empty, then n is a prime.

To test:

factors(12345); // [3, 5, 823]

This is a dumb brute force solution, but eh, it works. ¯\_(ツ)_/¯