forked from sksalahuddin2828/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlanetary_Weights.js
68 lines (60 loc) · 1.63 KB
/
Planetary_Weights.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const MERCURY_GRAVITY = 0.376;
const VENUS_GRAVITY = 0.889;
const MARS_GRAVITY = 0.378;
const JUPITER_GRAVITY = 2.36;
const SATURN_GRAVITY = 1.081;
const URANUS_GRAVITY = 0.815;
const NEPTUNE_GRAVITY = 1.14;
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
function isValidPlanet(planet) {
const validPlanets = [
"Mercury",
"Venus",
"Mars",
"Jupiter",
"Saturn",
"Uranus",
"Neptune",
];
return validPlanets.includes(planet);
}
function calculatePlanetWeight(earthWeight, planet) {
switch (planet) {
case "Mercury":
return earthWeight * MERCURY_GRAVITY;
case "Venus":
return earthWeight * VENUS_GRAVITY;
case "Mars":
return earthWeight * MARS_GRAVITY;
case "Jupiter":
return earthWeight * JUPITER_GRAVITY;
case "Saturn":
return earthWeight * SATURN_GRAVITY;
case "Uranus":
return earthWeight * URANUS_GRAVITY;
case "Neptune":
return earthWeight * NEPTUNE_GRAVITY;
default:
return 0;
}
}
function main() {
const earthWeight = parseFloat(prompt("Enter a weight on Earth:"));
let planet = prompt("Enter a planet:");
planet = capitalize(planet);
while (!isValidPlanet(planet)) {
if (planet === "Earth") {
alert("Please select a planet other than Earth.");
} else {
alert(`Error: ${planet} is not a planet.`);
}
planet = prompt("Enter a planet:");
planet = capitalize(planet);
}
const planetWeight = calculatePlanetWeight(earthWeight, planet);
const roundedWeight = planetWeight.toFixed(2);
alert(`The equivalent weight on ${planet}: ${roundedWeight}`);
}
main();