Exercism v3 launches on Sept 1st 2021. Learn more! 🚀🚀🚀
Avatar of rootulp

rootulp's solution

to Space Age in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Test suite


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.


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


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.


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.


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

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

  xit('age in earth years', function () {
    var age = new SpaceAge(1000000000);

  xit('age in mercury years', function () {
    var age = new SpaceAge(2134835688);

  xit('age in venus years', function () {
    var age = new SpaceAge(189839836);

  xit('age in mars years', function () {
    var age = new SpaceAge(2329871239);

  xit('age in jupiter years', function () {
    var age = new SpaceAge(901876382);

  xit('age in saturn years', function () {
    var age = new SpaceAge(3000000000);

  xit('age in uranus years', function () {
    var age = new SpaceAge(3210123456);

  xit('age in neptune year', function () {
    var age = new SpaceAge(8210123456);
function SpaceAge(seconds) {
  'use script';

  var earthTo = {
    Mercury: 0.2408467,
    Venus: 0.61519726,
    Mars: 1.8808158,
    Jupiter: 11.862615,
    Saturn: 29.447498,
    Uranus: 84.016846,
    Neptune: 164.79132

  function chompFloat(val) {
    return parseFloat(val.toFixed(2));

  function onEarth() {
    return seconds / 31557600;

  this.seconds = seconds;

  this.onEarth = function() {
    return chompFloat(onEarth());

  this.onMercury = function() {
    return chompFloat(onEarth() / earthTo['Mercury']);

  this.onVenus = function() {
    return chompFloat(onEarth() / earthTo['Venus']);

  this.onMars = function() {
    return chompFloat(onEarth() / earthTo['Mars']);

  this.onJupiter = function() {
    return chompFloat(onEarth() / earthTo['Jupiter']);

  this.onSaturn = function() {
    return chompFloat(onEarth() / earthTo['Saturn']);

  this.onUranus = function() {
    return chompFloat(onEarth() / earthTo['Uranus']);

  this.onNeptune = function() {
    return chompFloat(onEarth() / earthTo['Neptune']);


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?