forked from LaunchCodeEducation/Launch-Checklist-Form
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
191 lines (173 loc) · 6.99 KB
/
script.js
File metadata and controls
191 lines (173 loc) · 6.99 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Write your JavaScript code here!
// NOTE: req = request, res = response
window.addEventListener("load", () => {
let fuelReady = false;
let cargoReady = false;
let fieldCheck;
// TODO: Why does "this" get added to "fetch"? (OK to get rid of "this")
fetch("https://handlers.education.launchcode.org/static/planets.json")
.then((res) => {
res.json().then((json) => {
let index = Math.floor(Math.random() * json.length);
console.log(json[index].name); // not sure why this keeps adding "this"
// TODO: Try doing this DOM style later!
// TODO: Why is VSCode adding "this" to "document"! (So annoying!)
// BACKTICKS FTW!
document.getElementById("missionTarget").innerHTML = `
<h2>Mission Destination</h2>
<ol>
<li>Name: ${json[index].name}</li>
<li>Diameter: ${json[index].diameter}</li>
<li>Star: ${json[index].star}</li>
<li>Distance from Earth: ${json[index].distance}</li>
<li>Number of Moons: ${json[index].moons}</li>
</ol>
<img src="${json[index].image}">
`;
});
});
document.querySelector("form").addEventListener("submit", (event) =>{
event.preventDefault();
// Inputs
let pilotName = document.querySelector("input[name=pilotName]");
let copilotName = document.querySelector("input[name=copilotName]");
let fuelLevel = document.querySelector("input[name=fuelLevel]");
let cargoMass = document.querySelector("input[name=cargoMass]");
// Outputs
let pilotStatus = document.getElementById("pilotStatus");
let copilotStatus = document.getElementById("copilotStatus");
let fuelStatus = document.getElementById("fuelStatus");
let cargoStatus = document.getElementById("cargoStatus");
// Other stuff
let faultyItems = document.getElementById("faultyItems");
let launchStatus = document.getElementById("launchStatus");
// Update status
// Problem: after clicking submit, and typing again, pilotStatus is "null"?
pilotStatus.innerText = `Pilot ${pilotName.value} Ready`;
copilotStatus.innerText = `Co-Pilot ${copilotName.value} Ready`;
fuelStatus.innerText = `Fuel Level high enough for launch`;
cargoStatus.innerText = `Cargo Level high enough for launch`;
// Update our booleans
fieldCheck = true;
cargoReady = false;
fuelReady = false;
// Validate!
// NOTE: HTML5 has a required attribute
// TODO: Might need to do an Array.from()
// What happens here?
// Instead of doing a boolean test on each value, I chose to have a some test each value
// some gives us a function to use instead of ||
if([pilotName,copilotName,fuelLevel,cargoMass].some(field => field.value.trim() === "")){
window.alert("All fields are required!");
fieldCheck = false;
}
// TODO: Add placeholders?
// pilotname and copiolot can't be numbers
// You know what could fix this? REGEXP!
// You know what, let's just do this instead.
// typeof String(pilotName.value) !== "string" && !isNaN(pilotName.value)
if(!isNaN(pilotName.value)){
// regexp
window.alert("Please enter a name in the Pilot field");
fieldCheck = false;
}
// typeof String(copilotName.value) !== "string" && !isNaN(copilotName.value)
if(!isNaN(copilotName.value)){
// regexp
window.alert("Please enter a name in the Co-Pilot field");
fieldCheck = false;
}
if(typeof Number(fuelLevel.value) !== "number" || isNaN(Number(fuelLevel.value))){
window.alert("Please enter a number in the Fuel Level field");
fieldCheck = false;
}
if(typeof Number(cargoMass.value) !== "number" || isNaN(Number(cargoMass.value))){
window.alert("Please enter a number in the Cargo Mass field");
fieldCheck = false;
}
if(!fieldCheck){
return; // get out of this event
}
if(Number(fuelLevel.value) < 10000){
// We are no go for launch. Somebody forgot gas!
faultyItems.style.setProperty("visibility","visible");
faultyItems.innerHTML = `There is not enough fuel for the mission! We have ${fuelLevel.value}L. We need at least 10,000L!`;
launchStatus.style.setProperty("color","red");
launchStatus.innerText = "Shuttle not ready for launch!";
fuelReady = false;
return; // get out of this event
}
else{
fuelReady = true;
}
if(Number(cargoMass.value) > 10000){
// We are no go for launch. Something needs to be left behind.
faultyItems.style.setProperty("visibility","visible");
faultyItems.innerHTML = `There is too much mass for the shuttle to take off! We have ${cargoMass.value}kg. We can not carry more than 10,000kg!`;
launchStatus.style.setProperty("color","red");
launchStatus.innerText = "Shuttle not ready for launch!";
cargoReady = false;
return; // get out of this event
}
else{
cargoReady = true;
}
// lets do an every instead of a &&
if([fuelReady,cargoReady,fieldCheck].every(ready => ready === true)){
// We are go for launch!
launchStatus.style.setProperty("color","green");
launchStatus.innerText = "Shuttle is ready for launch!";
}
});
// status box should not update unless all the fields are filled
// and meet the requirements
/*
// TODO: We will need to add some focus features to reset some stuff
let pilotName = document.querySelector("input[name=pilotName]");
let copilotName = document.querySelector("input[name=copilotName]");
let fuelLevel = document.querySelector("input[name=fuelLevel]");
let cargoMass = document.querySelector("input[name=cargoMass]");
function focusAction(field){
// changes the styles
}
function blurAction(field){
// changes the styles
}
pilotName.addEventListener("focus",(event) => {
focusAction(this);
});
pilotName.addEventListener("click",(event) => {
focusAction(this);
});
pilotName.addEventListener("blur",(event) => {
blurAction(this);
});
copilotName.addEventListener("focus",(event) => {
focusAction(this);
});
copilotName.addEventListener("click",(event) => {
focusAction(this);
});
copilotName.addEventListener("blur",(event) => {
blurAction(this);
});
fuelLevel.addEventListener("focus",(event) => {
focusAction(this);
});
fuelLevel.addEventListener("click",(event) => {
focusAction(this);
});
fuelLevel.addEventListener("blur",(event) => {
blurAction(this);
});
cargoMass.addEventListener("focus",(event) = > {
focusAction(this);
});
cargoMass.addEventListener("click",(event) = > {
focusAction(this);
});
cargotMass.addEventListener("blur",(event) => {
blurAction(this);
});
*/
});