Avatar of w1zeman1p

w1zeman1p's solution

to Space Age in the JavaScript 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 JavaScript to install the necessary dependencies:

http://exercism.io/languages/javascript/installation

Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine space-age.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.

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.spec.js

var SpaceAge = require('./space-age');

describe('Space Age', function () {
  it('age in seconds', function () {
    var age = new SpaceAge(1000000);
    expect(age.seconds).toEqual(1000000);
  });

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

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

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

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

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

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

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

  xit('age in neptune year', function () {
    var age = new SpaceAge(8210123456);
    expect(age.onEarth()).toEqual(260.16);
    expect(age.onNeptune()).toEqual(1.58);
  });
});
'use strict';

const earth = 31557600;
class SpaceAge {
  constructor(seconds) {
    this.seconds = seconds;
  }

  onEarth() {
    return round2(this.seconds / earth);
  }

  onMercury() {
    return round2(this.seconds / (earth * 0.2408467));
  }

  onVenus() {
    return round2(this.seconds / (earth * 0.61519726));
  }

  onMars() {
    return round2(this.seconds / (earth * 1.8808158));
  }

  onJupiter() {
    return round2(this.seconds / (earth * 11.862615));
  }

  onSaturn() {
    return round2(this.seconds / (earth * 29.447498));
  }

  onUranus() {
    return round2(this.seconds / (earth * 84.016846));
  }

  onNeptune() {
    return round2(this.seconds / (earth * 164.79132));
  }
}

function round2(num) {
  return Math.round(num * 100) / 100;
}

module.exports = 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?