Given an age in seconds, calculate how old someone would be on:
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:
https://exercism.io/tracks/javascript/installation
Install assignment dependencies:
$ npm install
Execute the tests with:
$ npm test
In the test suites all tests but the first have been skipped.
Once you get a test passing, you can enable the next one by changing xtest
to
test
.
Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=01
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
import { age } from './space-age';
describe('Space Age', () => {
test('age on Earth', () => {
expect(age('earth', 1000000000)).toEqual(31.69);
});
xtest('age on Mercury', () => {
expect(age('mercury', 2134835688)).toEqual(280.88);
});
xtest('age on Venus', () => {
expect(age('venus', 189839836)).toEqual(9.78);
});
xtest('age on Mars', () => {
expect(age('mars', 2129871239)).toEqual(35.88);
});
xtest('age on Jupiter', () => {
expect(age('jupiter', 901876382)).toEqual(2.41);
});
xtest('age on Saturn', () => {
expect(age('saturn', 2000000000)).toEqual(2.15);
});
xtest('age on Uranus', () => {
expect(age('uranus', 1210123456)).toEqual(0.46);
});
xtest('age on Neptune', () => {
expect(age('neptune', 1821023456)).toEqual(0.35);
});
});
const EARTH_YEAR_DURATION_SECONDS = 31557600
const EARTH_YEAR_FACTORS = {
earth: 1.0,
jupiter: 11.862615,
mars: 1.8808158,
mercury: 0.2408467,
neptune: 164.79132,
saturn: 29.447498,
uranus: 84.016846,
venus: 0.61519726,
}
export const age = (planet, seconds) =>
Number(((seconds / EARTH_YEAR_DURATION_SECONDS) / EARTH_YEAR_FACTORS[planet]).toFixed(2))
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.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments