-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptHelper.js
More file actions
105 lines (85 loc) · 3.89 KB
/
scriptHelper.js
File metadata and controls
105 lines (85 loc) · 3.89 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
require('isomorphic-fetch');
function addDestinationInfo(document, name, diameter, star, distance, moons, imageUrl) {
let div = document.getElementbyId("missionTarget");
div.innerHTML =
`<h2>Mission Destination</h2>
<ol>
<li>Name: ${name}</li>
<li>Diameter: ${diameter}</li>
<li>Star: ${star}</li>
<li>Distance from Earth: ${distance}</li>
<li>Number of Moons: ${moons}</li>
</ol>
<img src="${imageUrl}">`;
}
function validateInput(testInput) {
let numberInput = Number(testInput);
if (testInput === " ")
{
return "Empty";
}
else if (isNan(numberInput))
{
return "Not a number";
}
else if (isNan(numberInput) === false)
return "Is a number";
}
function formSubmission(document, list, pilot, copilot, fuelLevel, cargoLevel) {
let pilotStatus = document.getElementbyId("pilotStatus");
let copilotStatus = document.getElementbyId("copilotStatus");
let fuel = document.getElementbyId("fuelStatus");
let cargo = document.getElementbyId("cargoStatus");
if (validateInput(pilot) === "Empty" || validateInput(copilot) === "Empty" || validateInput(fuelLevel) === "Empty" || validateInput(cargoLevel) === "Empty") {
alert ("Missing value!");
} else if (validateInput(pilot) === "Is a Number" || validateInput(copilot) === "Is a Number" || validateInput(fuelLevel) === "Not a Number" || validateInput(cargoLevel) === "Not a Number") {
alert ("Please enter a valid value for each field!");
} else {
list.style.visibility = "visible";
pilotStatus.innerHTML = `Pilot ${pilot} is ready`;
copilotStatus.innerHTML = `Copilot ${copilot} is ready`;
let launchStatus = document.getElementbyId("launchStatus");
if (fuelLevel < 10000 && cargoLevel <= 10000) {
fuel.innerHTML = "Fuel level is too low for liftoff";
cargo.innerHTML = "Cargo mass is too low for liftoff";
launchStatus.innerHTML = "Shuttle not ready for launch";
launchStatus.style.color = "red";
} else if (fuelLevel >= 10000 && cargoLevel >= 10000) {
fuel.innerHTML = "Fuel level is good";
cargo.innerHTML = "Cargo is too heavy for liftoff";
launchStatus.innerHTML = "Shuttle is not ready for launch";
launchStatus.style.color = "red";
} else if (fuelLevel < 10000 && cargoLevel > 10000) {
fuel.innerHTML = "Fuel level is too low for liftoff";
cargo.innerHTML = "Cargo is too heavy for liftoff";
launchStatus.innerHTML = "Shuttle is not ready for launch";
launchStatus.style.color = "red";
} else {
fuel.innerHTML = "Fuel level is good"
cargo.innerHTML = "Cargo mass is good"
launchStatus.innerHTML = "Shuttle is ready for launch"
launchStatus.style.color = "green";
}
}
}
async function myFetch() {
let planetsReturned;
planetsReturned = await fetch ("https://handlers.education.launchcode.org/static/planets.json").then(function(response) {
if (response.status >= 400) {
throw new error ("Bad response");
}
else {
return response.json();
}
});
return planetsReturned;
}
function pickPlanet(planets) {
let index = Math.floor(Math.random()*planets.length);
return planets[index];
}
module.exports.addDestinationInfo = addDestinationInfo;
module.exports.validateInput = validateInput;
module.exports.formSubmission = formSubmission;
module.exports.pickPlanet = pickPlanet;
module.exports.myFetch = myFetch;