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
143 lines (124 loc) · 5.67 KB
/
script.js
File metadata and controls
143 lines (124 loc) · 5.67 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
// Write your JavaScript code here!
window.addEventListener("load", function(){
const form = document.querySelector("form")
const faultyItems = document.querySelector("#faultyItems")
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]");
let pilotStatus = document.getElementById("pilotStatus");
let copilotStatus = document.getElementById("copilotStatus");
let fuelStatus = document.getElementById("fuelStatus");
let cargoStatus = document.getElementById("cargoStatus");
let launchStatus = document.getElementById("launchStatus");
//Fetch planetary JSON data
fetch("https://handlers.education.launchcode.org/static/planets.json").then(function(response){
response.json().then(function(json){
const missionTarget = document.getElementById("missionTarget");
let index = Math.floor(Math.random()*json.length);
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}"></img>`
})
})
form.addEventListener("submit", function(){
// 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]");
// let pilotStatus = document.getElementById("pilotStatus");
// let copilotStatus = document.getElementById("copilotStatus");
// let fuelStatus = document.getElementById("fuelStatus");
// let cargoStatus = document.getElementById("cargoStatus");
// let launchStatus = document.getElementById("launchStatus");
//End user validation alerts
//Requires Pilot Name
if(pilotName.value === ""){
alert("Pilot Name is required.");
event.preventDefault();
}
//Requires Copilot Name
if(copilotName.value === ""){
alert("Copilot Name is required.");
event.preventDefault();
}
//Requires Fuel Level
if(fuelLevel.value === ""){
alert("Fuel Level is required.");
event.preventDefault();
}
//Requires Fuel Level to be a number
if(isNaN(fuelLevel.value)){
alert("Fuel Level must be a number.")
event.preventDefault();
}
//Requires Cargo Mass
if(cargoMass.value === ""){
alert("Cargo Mass is required.");
event.preventDefault();
}
//Requires Cargo Mass to be a number
if(isNaN(cargoMass.value)){
alert("Cargo Mass must be a number.")
event.preventDefault();
}
//Makes h2 "launchStatus" "Shuttle is ready for launch" and color = green when all elements are correct
if(Number(fuelLevel.value) >= 10000 && Number(cargoMass.value) <= 10000){
faultyItems.style.visibility = "visible";
launchStatus.innerHTML = `Shuttle is ready for launch`;
launchStatus.style.color = "green";
event.preventDefault();
}
//Makes Faulty Items Visible && h2 "launchStatus" changes to "Shuttle not ready for launch" and color = red when fuelLevel<10000
if(Number(fuelLevel.value) < 10000){
faultyItems.style.visibility = "visible";
pilotStatus.innerHTML = `Pilot ${pilotName.value} Ready`;
copilotStatus.innerHTML = `Copilot ${copilotName.value} Ready`;
fuelStatus.innerHTML = `Not enough fuel for the journey`;
cargoStatus.innerHTML = `Cargo mass low enough for launch`;
launchStatus.innerHTML = `Shuttle not ready for launch`
launchStatus.style.color = "red";
event.preventDefault();
}
//Makes Faulty Items Visible && h2 "launchStatus" changes to "Shuttle not ready for launch" and color = red when cargoMass>10000
if(Number(cargoMass.value) > 10000){
faultyItems.style.visibility = "visible";
pilotStatus.innerHTML = `Pilot ${pilotName.value} Ready`;
copilotStatus.innerHTML = `Copilot ${copilotName.value} Ready`;
fuelStatus.innerHTML = `Fuel level high enough for launch`;
cargoStatus.innerHTML = `Too much weight for the journey`;
launchStatus.innerHTML = `Shuttle not ready for launch`
launchStatus.style.color = "red";
event.preventDefault();
}
//Makes Faulty Items Visible && h2 "launchStatus" changes to "Shuttle not ready for launch" and color = red
if(Number(fuelLevel.value)<10000 && Number(cargoMass.value)>10000){
faultyItems.style.visibility = "visible";
pilotStatus.innerHTML = `Pilot ${pilotName.value} Ready`;
copilotStatus.innerHTML = `Copilot ${copilotName.value} Ready`;
fuelStatus.innerHTML = `Not enough fuel for the journey`;
cargoStatus.innerHTML = `Too much weight for the journey`;
launchStatus.innerHTML = `Shuttle not ready for launch`
launchStatus.style.color = "red";
event.preventDefault();
}
})
})
/* This block of code shows how to format the HTML once you fetch some planetary JSON!
<h2>Mission Destination</h2>
<ol>
<li>Name: ${}</li>
<li>Diameter: ${}</li>
<li>Star: ${}</li>
<li>Distance from Earth: ${}</li>
<li>Number of Moons: ${}</li>
</ol>
<img src="${}">
*/