MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / age

Function age

Exercism_Problems/space_age/solution.js:2–45  ·  view source on GitHub ↗
(planet, age)

Source from the content-addressed store, hash-verified

1
2const age = (planet, age) => {
3 const earth = 365.25*24*60*60; //total seconds in a year
4
5 // const list of orbital period of planets in Earth years
6 const mercury = 0.2408467 ;
7 const venus = 0.61519726;
8 const mars = 1.8808158;
9 const jupiter = 11.862615;
10 const saturn = 29.447498;
11 const uranus = 84.016846;
12 const neptune = 164.79132;
13 let planet_age = 0;
14 // check for "planets" value and calculate the age base on given "age" in seconds
15 switch (planet) {
16 case 'earth':
17 planet_age = age/earth
18 break;
19 case 'mercury':
20 planet_age = (age/earth)/mercury
21 break;
22 case 'venus':
23 planet_age = (age/earth)/venus
24 break;
25 case 'mars':
26 planet_age = (age/earth)/mars
27 break;
28 case 'jupiter':
29 planet_age = (age/earth)/jupiter
30 break;
31 case 'saturn':
32 planet_age = (age/earth)/saturn
33 break;
34 case 'uranus':
35 planet_age = (age/earth)/uranus
36 break;
37 case 'neptune':
38 planet_age = (age/earth)/neptune
39 break;
40 default:
41 break;
42 }
43 let your_age = planet_age.toFixed(2) // reduce the floating value to 2 decimal place
44 return Number(your_age); //return age
45};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected