Are you considering a job as a JavaScript Developer? Today’s companies want to be sure you have the skills and so their interview process is more than just “meet and greets” and “nebulous questions”. Today’s technical interview will often include some form of programming test.

I recently had the chance to participate in one of these technical interviews at Elastic. It was a lot of fun and exposed some strengths and weaknesses.

Do you want to see how quickly you can code the solution?

The test consists of three files:

  1. a simple HTML page
  2. a JavaScript file for you to write your code
  3. a javascript validation file

The JavaScript file for your to edit has an empty function where you write your code. This function is called by the validation with ‘input’ and will test the return. Download all three files and place them in a folder on your local machine. Edit teh file for your code. Once you have written your solution, open the local HTML file in a browser to see the results of the validation!

HTML File (index.html):

<!DOCTYPE HTML>
<html>
  <head>
    <title>Elastic Coding Exercise</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.2.5/mocha.css" rel="stylesheet" />
  </head>
  <body>
    <div id="mocha"></div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.2.5/mocha.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/chai/2.3.0/chai.js"></script>
    <script src="flatten.js"></script>
    <script src="test.flatten.js"></script>
  </body>
</html>

Validation File (test.flatten.js):

mocha.setup('bdd');
const expect = chai.expect;

describe('flatten()', function () {
  it('should flatten a simple nested object', function () {
    const source = {
      foo: {
        bar: 1
      }
    };

    const expected = {
      'foo.bar': 1
    };

    expect(flatten(source)).to.deep.equal(expected);
  });

  it('should flatten an object with nested arrays', function () {
    const source = {
      foo: [{
        bar: 1
      }, {
        bar: 2
      }]
    };

    const expected = {
      'foo.0.bar': 1,
      'foo.1.bar': 2
    };

    expect(flatten(source)).to.deep.equal(expected);
  });

  it('should flatten a complex nested object', function () {
    const source = {
      a: 1,
      b: {
        c: true,
        d: {
          e: 'foo'
        }
      },
      f: false,
      g: ['red', 'green', 'blue'],
      h: [{
        i: 2,
        j: 3
      }]
    };

    const expected = {
      'a': 1,
      'b.c': true,
      'b.d.e': 'foo',
      'f': false,
      'g.0': 'red',
      'g.1': 'green',
      'g.2': 'blue',
      'h.0.i': 2,
      'h.0.j': 3
    };

    expect(flatten(source)).to.deep.equal(expected);
  });
});

mocha.run();

Your Code (flatten.js):

/**
 * Takes a deeply nested object, `source`, and returns an object with
 * dot-separated paths pointing to all primitive values from `source`.
 *
 * Examples:
 *
 *   flatten({ foo: { bar: 1 } })
 *     //=> { 'foo.bar': 1 }
 *
 *   flatten({ foo: [{ bar: 1 }, { bar: 2 }] })
 *     //=> { 'foo.0.bar': 1, 'foo.1.bar': 2 }
 */
function flatten(source) {
  // TODO: Implement this function
}

Leave a Reply

Your email address will not be published. Required fields are marked *