Avatar of PatrickMcSweeny

PatrickMcSweeny's solution

to Gigasecond in the JavaScript Track

Published at Aug 18 2019 · 0 comments
Instructions
Test suite
Solution

Given a moment, determine the moment that would be after a gigasecond has passed.

A gigasecond is 10^9 (1,000,000,000) seconds.

Setup

Go through the setup instructions for Javascript to install the necessary dependencies:

https://exercism.io/tracks/javascript/installation

Requirements

Install assignment dependencies:

$ npm install

Making the test suite pass

Execute the tests with:

$ npm test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing xtest to test.

Source

Chapter 9 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=09

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

gigasecond.spec.js

import { gigasecond } from './gigasecond';

describe('Gigasecond', () => {
  test('tells a gigasecond anniversary since midnight', () => {
    const gs = gigasecond(new Date(Date.UTC(2015, 8, 14)));
    const expectedDate = new Date(Date.UTC(2047, 4, 23, 1, 46, 40));
    expect(gs).toEqual(expectedDate);
  });

  xtest('tells the anniversary is next day when you are born at night', () => {
    const gs = gigasecond(new Date(Date.UTC(2015, 8, 14, 23, 59, 59)));
    const expectedDate = new Date(Date.UTC(2047, 4, 24, 1, 46, 39));
    expect(gs).toEqual(expectedDate);
  });

  xtest('even works before 1970 (beginning of Unix epoch)', () => {
    const gs = gigasecond(new Date(Date.UTC(1959, 6, 19, 5, 13, 45)));
    const expectedDate = new Date(Date.UTC(1991, 2, 27, 7, 0, 25));
    expect(gs).toEqual(expectedDate);
  });
});
const GIGASECOND = 10 ** 9;

export const gigasecond = moment =>
  new Date(moment.getTime() + GIGASECOND * 1000);

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?