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

rootulp's solution

to Space Age in the TypeScript Track

Published at Jul 13 2018 · 0 comments
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.

Given an age in seconds, calculate how old someone would be on:

  • Earth: orbital period 365.25 Earth days, or 31557600 seconds
  • Mercury: orbital period 0.2408467 Earth years
  • Venus: orbital period 0.61519726 Earth years
  • Mars: orbital period 1.8808158 Earth years
  • Jupiter: orbital period 11.862615 Earth years
  • Saturn: orbital period 29.447498 Earth years
  • Uranus: orbital period 84.016846 Earth years
  • Neptune: orbital period 164.79132 Earth years

So if you were told someone were 1,000,000,000 seconds old, you should be able to say that they're 31.69 Earth-years old.

If you're wondering why Pluto didn't make the cut, go watch this youtube video.

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

Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=01

Submitting Incomplete Solutions

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

space-age.test.ts

import SpaceAge from './space-age'

describe('Space Age', () => {

  it('age in seconds', () => {
    const age = new SpaceAge(1000000)
    expect(age.seconds).toEqual(1000000)
  })

  xit('age in earth years', () => {
    const age = new SpaceAge(1000000000)
    expect(age.onEarth()).toEqual(31.69)
  })

  xit('age in mercury years', () => {
    const age = new SpaceAge(2134835688)
    expect(age.onEarth()).toEqual(67.65)
    expect(age.onMercury()).toEqual(280.88)
  })

  xit('age in venus years', () => {
    const age = new SpaceAge(189839836)
    expect(age.onEarth()).toEqual(6.02)
    expect(age.onVenus()).toEqual(9.78)
  })

  xit('age in mars years', () => {
    const age = new SpaceAge(2329871239)
    expect(age.onEarth()).toEqual(73.83)
    expect(age.onMars()).toEqual(39.25)
  })

  xit('age in jupiter years', () => {
    const age = new SpaceAge(901876382)
    expect(age.onEarth()).toEqual(28.58)
    expect(age.onJupiter()).toEqual(2.41)
  })

  xit('age in saturn years', () => {
    const age = new SpaceAge(3000000000)
    expect(age.onEarth()).toEqual(95.06)
    expect(age.onSaturn()).toEqual(3.23)
  })

  xit('age in uranus years', () => {
    const age = new SpaceAge(3210123456)
    expect(age.onEarth()).toEqual(101.72)
    expect(age.onUranus()).toEqual(1.21)
  })

  xit('age in neptune year', () => {
    const age = new SpaceAge(8210123456)
    expect(age.onEarth()).toEqual(260.16)
    expect(age.onNeptune()).toEqual(1.58)
  })
})
class SpaceAge {

  public seconds: number

    private ORBITAL_PERIOD_IN_EARTH_YEARS = Object.freeze({
        Mercury: 0.2408467,
        Venus: 0.61519726,
        Mars: 1.8808158,
        Jupiter: 11.862615,
        Saturn: 29.447498,
        Uranus: 84.016846,
        Neptune: 164.79132
    })

    private SECONDS_IN_EARTH_YEAR = 31557600

  constructor(seconds: number) {
    this.seconds = seconds
  }

  private earthYears(): number {
    return this.seconds / this.SECONDS_IN_EARTH_YEAR
  }

  public onEarth(): number {
    return SpaceAge.roundToTwoDecimalPlaces(this.earthYears())
  }

  public onMercury(): number {
    return SpaceAge.roundToTwoDecimalPlaces(this.earthYears() / this.ORBITAL_PERIOD_IN_EARTH_YEARS.Mercury)
  }

  public onVenus(): number {
    return SpaceAge.roundToTwoDecimalPlaces(this.earthYears() / this.ORBITAL_PERIOD_IN_EARTH_YEARS.Venus)
  }

  public onMars(): number {
    return SpaceAge.roundToTwoDecimalPlaces(this.earthYears() / this.ORBITAL_PERIOD_IN_EARTH_YEARS.Mars)
  }

  public onJupiter(): number {
    return SpaceAge.roundToTwoDecimalPlaces(this.earthYears() / this.ORBITAL_PERIOD_IN_EARTH_YEARS.Jupiter)
  }

  public onSaturn(): number {
    return SpaceAge.roundToTwoDecimalPlaces(this.earthYears() / this.ORBITAL_PERIOD_IN_EARTH_YEARS.Saturn)
  }

  public onUranus(): number {
    return SpaceAge.roundToTwoDecimalPlaces(this.earthYears() / this.ORBITAL_PERIOD_IN_EARTH_YEARS.Uranus)
  }

  public onNeptune(): number {
    return SpaceAge.roundToTwoDecimalPlaces(this.earthYears() / this.ORBITAL_PERIOD_IN_EARTH_YEARS.Neptune)
  }

  private static roundToTwoDecimalPlaces(num: number): number {
    return Math.round(num * 100) / 100
  }

}

export default SpaceAge

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?