Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Gigasecond in the TypeScript Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Calculate the moment when someone has lived for 10^9 seconds.

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

Setup

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

http://exercism.io/languages/typescript

Requirements

Install assignment dependencies:

$ yarn install

Making the test suite pass

Execute the tests with:

$ yarn 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.test.ts

import Gigasecond from './gigasecond'

describe('Gigasecond', () => {

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

  xit('tells the anniversary is next day when you are born at night', () => {
    const gs = new 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.date()).toEqual(expectedDate)
  })

  xit('even works before 1970 (beginning of Unix epoch )', () => {
    const gs = new 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.date()).toEqual(expectedDate)
  })

  xit('make sure calling "date" doesn\'t mutate value', () => {
    const gs = new Gigasecond(new Date(Date.UTC(1959, 6, 19, 5, 13, 45)))
    const expectedDate = new Date(Date.UTC(1991, 2, 27, 7, 0, 25))
    gs.date()
    expect(gs.date()).toEqual(expectedDate)
  })
})
class Gigasecond {

  date_of_birth: Date

  constructor(date_of_birth: Date) {
    this.date_of_birth = date_of_birth
  }

  public date() {
    return this.date_of_birth_plus_gigasecond()
  }

  private date_of_birth_plus_gigasecond() {
    return new Date(this.date_of_birth.getTime() + 1e12)
  }

}

export default Gigasecond

Community comments

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

+1 for clear naming

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?