Space age
This commit is contained in:
22
typescript/space-age/space-age.ts
Normal file
22
typescript/space-age/space-age.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
const earthYears = {
|
||||
mercury: 0.2408467,
|
||||
venus: 0.61519726,
|
||||
earth: 1.0,
|
||||
mars: 1.8808158,
|
||||
jupiter: 11.862615,
|
||||
saturn: 29.447498,
|
||||
uranus: 84.016846,
|
||||
neptune: 164.79132,
|
||||
} as const;
|
||||
|
||||
type Planet = keyof typeof earthYears;
|
||||
|
||||
const roundTwoDecimals = (num: number): number => Math.round(num * 100) / 100;
|
||||
|
||||
export function age(planet: Planet, seconds: number): number {
|
||||
const minutes = seconds / 60;
|
||||
const hours = minutes / 60;
|
||||
const days = hours / 24;
|
||||
const years = days / 365.25;
|
||||
return roundTwoDecimals(years / earthYears[planet]);
|
||||
}
|
||||
Reference in New Issue
Block a user