# paulfioravanti's solution

## to Space Age in the Elm Track

Published at Jun 24 2019 · 0 comments
Instructions
Test suite
Solution

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.

## Elm Installation

Refer to the Installing Elm page for information about installing elm.

## Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

``````\$ elm-test
``````

Automatically run tests again when you save changes:

``````\$ elm-test --watch
``````

As you work your way through the test suite, be sure to remove the `skip <|` calls from each test until you get them all passing!

## 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 is possible to submit an incomplete solution so you can see how others have completed the exercise.

### Tests.elm

``````module Tests exposing (tests)

import Expect exposing (FloatingPointTolerance(..))
import SpaceAge exposing (Planet(..), ageOn)
import Test exposing (..)

tests : Test
tests =
describe "SpaceAge"
[ test "31.7 Earth years in Earth years" <|
\() -> Expect.within (Absolute 0.01) 31.69 (ageOn Earth 1000000000.0)
, skip <|
test "2 Earth years in Earth years" <|
\() -> Expect.within (Absolute 0.01) 2 (ageOn Earth 63115200.0)
, skip <|
test "67.6 Earth years in Mercury years" <|
\() -> Expect.within (Absolute 0.01) 280.88 (ageOn Mercury 2134835688.0)
, skip <|
test "2 Earth years in Mercury years" <|
\() -> Expect.within (Absolute 0.01) 8.3 (ageOn Mercury 63115200.0)
, skip <|
test "6 Earth years in Venus years" <|
\() -> Expect.within (Absolute 0.01) 9.78 (ageOn Venus 189839836.0)
, skip <|
test "2 Earth years in Venus years" <|
\() -> Expect.within (Absolute 0.01) 3.25 (ageOn Venus 63115200.0)
, skip <|
test "67.6 Earth years in Mars years" <|
\() -> Expect.within (Absolute 0.01) 35.88 (ageOn Mars 2129871239.0)
, skip <|
test "2 Earth years in Mars years" <|
\() -> Expect.within (Absolute 0.01) 1.06 (ageOn Mars 63115200.0)
, skip <|
test "28.6 Earth years in Jupiter years" <|
\() -> Expect.within (Absolute 0.01) 2.41 (ageOn Jupiter 901876382.0)
, skip <|
test "2 Earth years in Jupiter years" <|
\() -> Expect.within (Absolute 0.01) 0.17 (ageOn Jupiter 63115200.0)
, skip <|
test "63.5 Earth years in Saturn years" <|
\() -> Expect.within (Absolute 0.01) 2.15 (ageOn Saturn 2000000000.0)
, skip <|
test "2 Earth years in saturn years" <|
\() -> Expect.within (Absolute 0.01) 0.07 (ageOn Saturn 63115200.0)
, skip <|
test "38.3 Earth years in Uranus years" <|
\() -> Expect.within (Absolute 0.01) 0.46 (ageOn Uranus 1210123456.0)
, skip <|
test "2 Earth years in uranus years" <|
\() -> Expect.within (Absolute 0.01) 0.02 (ageOn Uranus 63115200.0)
, skip <|
test "57.7 Earth years in Neptune years" <|
\() -> Expect.within (Absolute 0.01) 0.35 (ageOn Neptune 1821023456.0)
, skip <|
test "2 Earth years in Neptune years" <|
\() -> Expect.within (Absolute 0.01) 0.01 (ageOn Neptune 63115200.0)
]``````
``````module SpaceAge exposing (Planet(..), ageOn)

type Planet
= Mercury
| Venus
| Earth
| Mars
| Jupiter
| Saturn
| Uranus
| Neptune

ageOn : Planet -> Float -> Float
ageOn planet seconds =
toEarthYears seconds / orbitalFactor planet

-- PRIVATE

toEarthYears : Float -> Float
toEarthYears seconds =
let
earthOrbitalPeriodInSeconds =
31557600
in
seconds / earthOrbitalPeriodInSeconds

orbitalFactor : Planet -> Float
orbitalFactor planet =
case planet of
Earth ->
1

Mercury ->
0.2408467

Venus ->
0.61519726

Mars ->
1.8808158

Jupiter ->
11.862615

Saturn ->
29.447498

Uranus ->
84.016846

Neptune ->
164.79132``````