-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
174 lines (173 loc) · 4.35 KB
/
Copy pathscript.js
File metadata and controls
174 lines (173 loc) · 4.35 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
/*
Author: Ashton Curl (He's so cool)
Description: Project 2.
*/
//-----------------
function calculate(operator)
{
//step 1: get user inputs
let num1 = parseFloat(document.querySelector("#num1").value);
let num2 = parseFloat(document.querySelector("#num2").value);
//step 2: validate them
if(isNaN(num1) || isNaN(num2))
{
alert("Please enter a valid number!");
return;
}
//step 3: call getResult
const result = getResult(num1, num2, operator);
//step 4: dispaly the results
displayCalculatorResult(num1, num2, operator, result);
}//end function calculate
//-----------------
function getResult(num1, num2, operator)
{
let result;
if(operator === "+")
{
result = num1 + num2;
}
else if(operator === "-")
{
result = num1 - num2;
}
else if(operator === "*")
{
result = num1 * num2;
}
else if(operator === "/")
{
if(num2 === 0)
{
result = "undefined you silly goose";
}
else
{
result = num1 / num2;
}
}
else
{
result = "undefined. How did you even do that?";
}
return result;
}
//-----------------
function displayCalculatorResult(num1, num2, operator, result)
{
document.querySelector("#result").value = result;
const history = document.querySelector("#history");
//creating a history to display
const newLi = document.createElement("li");
newLi.textContent = `${num1} ${operator} ${num2} = ${result}`;
history.appendChild(newLi);
}
//-----------------
function clearHistory()
{
const history = document.querySelector("#history");
history.innerHTML = "";
}
//-----------------
function clearCalculatorInputs()
{
document.querySelector("#num1").value = "";
document.querySelector("#num2").value = "";
document.querySelector("#result").value = "";
}
//-----------------
//Part 2: NATO
//-----------------
function chToNato(ch)
{
const nato =
{
"A": "Alfa",
"B": "Bravo",
"C": "Charlie",
"D": "Delta",
"E": "Echo",
"F": "Foxtrot",
"G": "Golf",
"H": "Hotel",
"I": "India",
"J": "Juliett",
"K": "Kilo",
"L": "Lima",
"M": "Mike",
"N": "November",
"O": "Oscar",
"P": "Papa",
"Q": "Quebec",
"R": "Romeo",
"S": "Sierra",
"T": "Tango",
"U": "Uniform",
"V": "Victor",
"W": "Whiskey",
"X": "X-ray",
"Y": "Yankee",
"Z": "Zulu",
"1": "One",
"2": "Two",
"3": "Three",
"4": "Four",
"5": "Five",
"6": "Six",
"7": "Seven",
"8": "Eight",
"9": "Nine",
"0": "Zero"
};
return nato[ch.toUpperCase()] || ch;
//||ch so that it takes symbles like "!"
//tertiary statement (trust me bro)
}
//-----------------
function wordToNato(word)
{
return word.trim().split("").map(chToNato).join(" ");
}
//-----------------
function sentenceToNato(sentence)
{
return sentence.trim().split(" ").map(wordToNato).join(" ");
//notice the difference on split
}
//-----------------
function verbalize()
{
let string = document.getElementById("inputString").value;
//gets input string
const result = sentenceToNato(string);
//gets result
document.querySelector("#natoResult").textContent = result;
//displays result
}
//-----------------
function clearNATOInputs()
{
document.querySelector("#inputString").value = "";
document.querySelector("#natoResult").textContent = "";
}//end function clearNATOInputs
//-----------------
function setup() {
//calculate section
//calculations
document.querySelector("#add").addEventListener("click", ()=> calculate("+"));
document.querySelector("#subtract").addEventListener("click", ()=> calculate("-"));
document.querySelector("#multiply").addEventListener("click", ()=> calculate("*"));
document.querySelector("#divide").addEventListener("click", ()=> calculate("/"));
//clearing
document.querySelector("#clearHistory").addEventListener("click", clearHistory);
document.querySelector("#clearCalculator").addEventListener("click", clearCalculatorInputs);
//-----------------------
//NATO section
//clearing
document.getElementById("clearNATO").addEventListener("click", clearNATOInputs);
//functionality
document.getElementById("verbalize").addEventListener("click", verbalize);
}
//-----------------
//An event listener for when the DOM content is loaded to initialize the setup function.
window.addEventListener("DOMContentLoaded", setup);