Avatar of freddie2025

freddie2025's solution

to Gigasecond in the JavaScript Track

Published at Mar 11 2020 · 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.

It is possible to return a correct value for this exercise by mutating the solution function argument. Although there are legitimate use cases for mutating function arguments, this is usually undesirable, and in the case of this exercise, clearly unexpected. For this reason, the test suite has a test that fails in case the argument has been modified after the function execution.

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', () => {  
  // date only specification of time
  test('tells a gigasecond anniversary since midnight', () => {
    const gs = gigasecond(new Date(Date.UTC(2011, 3, 25)));
    const expectedDate = new Date(Date.parse('2043-01-01T01:46:40Z'));
    expect(gs).toEqual(expectedDate);
  });
  
  // second test for date only specification of time
  xtest('tells another gigasecond anniversary since midnight', () => {
    const gs = gigasecond(new Date(Date.UTC(1977, 5, 13)));
    const expectedDate = new Date(Date.parse('2009-02-19T01:46:40Z'));
    expect(gs).toEqual(expectedDate);
  });
  
  // third test for date only specification of time
  xtest('tells gigasecond anniversary since midnight, from before UNIX epoch', () => {
    const gs = gigasecond(new Date(Date.UTC(1959, 6, 19)));
    const expectedDate = new Date(Date.parse('1991-03-27T01:46:40Z'));
    expect(gs).toEqual(expectedDate);
  });
  
  // full time specified
  xtest('tells the anniversary, including a time', () => {
    const gs = gigasecond(new Date(Date.UTC(2015, 0, 24, 22, 0, 0)));
    const expectedDate = new Date(Date.parse('2046-10-02T23:46:40Z'));
    expect(gs).toEqual(expectedDate);
  });
  
  // full time with day roll-over
  xtest('tells the anniversary is next day when you are born at night', () => {
    const gs = gigasecond(new Date(Date.UTC(2015, 0, 24, 23, 59, 59)));
    const expectedDate = new Date(Date.parse('2046-10-03T01:46:39Z'));
    expect(gs).toEqual(expectedDate);
  });

  xtest('does not mutate the input', () => {
    const input = new Date(Date.UTC(2020, 0, 4, 20, 28, 30));
    gigasecond(input);
    expect(input).toEqual(new Date(Date.UTC(2020, 0, 4, 20, 28, 30)));
  });
});
//
// This is only a SKELETON file for the 'Gigasecond' exercise. It's been provided as a
// convenience to get you started writing code faster.
//

export const gigasecond = (date) => {
  return new Date(date.getTime() + 1e12);
};

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?