forked from LaunchCodeEducation/Launch-Checklist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptHelper.js
More file actions
125 lines (108 loc) · 3.83 KB
/
scriptHelper.js
File metadata and controls
125 lines (108 loc) · 3.83 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Write your helper functions here!
require("isomorphic-fetch");
function addDestinationInfo(
document,
name,
diameter,
star,
distance,
moons,
image
) {
let target = document.getElementById("missionTarget");
target.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=${image}>
`;
}
function validateInput(testInput) {
if (testInput == "") {
return 'Empty';
}
if (isNaN(parseInt(testInput)) == false) {
return "Is a Number";
}
if (isNaN(testInput) == true) {
return "Not a Number";
}
}
function formSubmission(document, list, pilot, copilot, fuelLevel, cargoLevel) {
// console.log(list, pilot, copilot, fuelLevel, cargoLevel)
// let status = document.getElementById("launchStatus");
// let pilotStatus = document.getElementById("pilotStatus");
// let copilotStatus = document.getElementById("copilotStatus");
// let fuelStatus = document.getElementById("fuelStatus");
// let cargoStatus = document.getElementById("cargoStatus");
let returnObj = {
empty: false,
invalid: false,
}
// if (list === undefined){
// list = document.getElementById("faultyItems")
// }
if (
validateInput(pilot) === "Empty" ||
validateInput(copilot) === "Empty" ||
validateInput(fuelLevel) === "Empty" ||
validateInput(cargoLevel) === "Empty"
) {
returnObj.empty = true
console.error("All fields are required!");
return returnObj;
} else if (
validateInput(pilot) === "Is a Number" ||
validateInput(copilot) === "Is a Number" ||
validateInput(fuelLevel) === "Not a Number" ||
validateInput(cargoLevel) === "Not a Number"
) {
returnObj.invalid = true;
console.error("Make sure to enter valid information for each field!");
return returnObj;
}
if (fuelLevel > 10000 && cargoLevel< 10000) {
document.getElementById("launchStatus").innerText = "Shuttle is ready for launch";
document.getElementById("launchStatus").style.color = "rgb(65, 159, 106)";
document.getElementById("faultyItems").style.visibility = "hidden"
} else if (fuelLevel < 10000 || cargoLevel > 10000) {
document.getElementById("launchStatus").innerText = "Shuttle not ready for launch";
document.getElementById("launchStatus").style.color = "rgb(199, 37, 78)";
document.getElementById("faultyItems").style.visibility = "visible";
document.getElementById("pilotStatus").innerText = `Pilot ${pilot.value} Ready`;
document.getElementById("copilotStatus").innerText = `Co-pilot ${copilot.value} Ready`;
}
if (fuelLevel < 10000) {
document.getElementById("fuelStatus").innerText = "Fuel level too low for launch";
} else {
document.getElementById("fuelStatus").innerText = "Fuel level high enough for launch";
}
if (cargoLevel > 10000) {
document.getElementById("cargoStatus").innerText = "Cargo mass too high for launch";
} else {
document.getElementById("cargoStatus").innerText = "Cargo mass low enough for launch";
}
return returnObj;
}
async function myFetch() {
let planetsReturned;
planetsReturned = await fetch(
"https://handlers.education.launchcode.org/static/planets.json"
).then(function (response) {
return response.json();
});
return planetsReturned;
}
function pickPlanet(planets) {
return planets[Math.floor(Math.random() * planets.length)];
}
module.exports.addDestinationInfo = addDestinationInfo;
module.exports.validateInput = validateInput;
module.exports.formSubmission = formSubmission;
module.exports.pickPlanet = pickPlanet;
module.exports.myFetch = myFetch;