-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.html
273 lines (242 loc) · 17.5 KB
/
code.html
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<!doctype html>
<html lang="en" class="h-100">
<head>
<title>FizzBuzz: A Coding Project by Julian Macioce</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<script src="https://kit.fontawesome.com/5db21ba9c6.js" crossorigin="anonymous"></script>
<link href="css/site.css" rel="stylesheet">
<link rel="stylesheet" href="https://use.typekit.net/ijx1sel.css">
<link href="css/prism.css" rel="stylesheet">
<link rel="icon" type="image/png" href="img/favicon-32x32.png">
</head>
<body class="d-flex flex-column h-100">
<!-- Nav Section -->
<nav class="navbar navbar-expand-md navbar-dark fixed-top">
<div class="container-fluid">
<a class="navbar-brand logoFont" href="app.html"><img src="img/favicon-32x32.png" class="d-inline-block align-text-top" title="My Favourite Pokémon: Snorlax!" height="26"> FIZZBUZZ</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse"
aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav me-auto mb-2 mb-md-0">
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="app.html">The App</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="code.html">The Code</a>
</li>
<li class="nav-item">
<a class="nav-link" target="_blank" href="https://github.com/JMacioce/FizzBuzz">Git Repo</a>
</li>
<li class="nav-item">
<a class="nav-link" target="_blank" href="https://julianmacioce.me/">About</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- Main Section -->
<main class="flex-shrink-0">
<div class="container py-5 px-5 mt-5">
<h2 class="border-1 border-bottom border-dark">The Code for <span class="logoFont">FizzBuzz</span></h2>
<div class="row row-cols-1 row-cols-lg-2">
<div class="col-lg-8">
<pre class="line-numbers">
<code class="language-javascript">
//get fizz and buzz values from the user
//controller function
function getValues() {
//get user values from page
let fizzValue = document.getElementById("fizzValue").value;
let buzzValue = document.getElementById("buzzValue").value;
//parse for integers
fizzValue = parseInt(fizzValue);
buzzValue = parseInt(buzzValue);
//check that the values are integers
if (Number.isInteger(fizzValue) && Number.isInteger(buzzValue)) {
//call FizzBuzz
let fbArray = fizzBuzzC(fizzValue, buzzValue);
//call displayValues
displayValues(fbArray);
}
else {
alert("You must enter integersg!");
}
}
//For-Loop Solution
//logic function
function fizzBuzzA(fizzValue, buzzValue) {
//initialize empty array
let returnArray = [];
//loop from 1 to 100
for (let i = 1; i <= 100; i++) {
//check to see if divisible by both fizz and buzz value
if(i % fizzValue == 0 && i % buzzValue == 0) {
returnArray.push('FizzBuzz');
}
//check to see if divisible by fizz value
else if(i % fizzValue == 0) {
returnArray.push('Fizz');
}
//check to see if divisible by fizz value
else if(i % buzzValue == 0) {
returnArray.push('Buzz');
}
else {
returnArray.push(i);
}
}
return returnArray;
}
//Switch Statement Solution
//logic function
function fizzBuzzB(fizzValue, buzzValue) {
let returnArray = [];
let Fizz = false;
let Buzz = false;
for (let i = 1; i <= 100; i++) {
Fizz = i % fizzValue == 0;
Buzz = i % buzzValue == 0;
switch(true) {
case Fizz && Buzz: {
returnArray.push('FizzBuzz');
break;
}
case Fizz: {
returnArray.push('Fizz');
break;
}
case Buzz: {
returnArray.push('Buzz');
break;
}
default: {
returnArray.push(i);
break;
}
}
}
return returnArray;
}
//Ternary Operator Solution
//logic function
function fizzBuzzC(fizzValue, buzzValue) {
let returnArray = [];
for (let i = 1; i <= 100; i++) {
let value = ((i % fizzValue == 0 ? 'Fizz' : '') + (i % buzzValue == 0 ? 'Buzz' : '') || i)
returnArray.push(value);
}
return returnArray;
}
//Display reversed string to user
//view function
function displayValues(fbArray) {
//get table body element from the page
let tableBody = document.getElementById("results");
//get template row
let templateRow = document.getElementById("fbTemplate");
//clear table first
tableBody.innerHTML = "";
for (let i = 0; i < fbArray.length; i += 5) {
let tableRow = document.importNode(templateRow.content, true);
//grab all the td tags to put in the array
let rowCols = tableRow.querySelectorAll("td");
rowCols[0].classList.add(fbArray[i]);
rowCols[0].textContent = fbArray[i];
rowCols[1].classList.add(fbArray[i+1]);
rowCols[1].textContent = fbArray[i+1];
rowCols[2].classList.add(fbArray[i+2]);
rowCols[2].textContent = fbArray[i+2];
rowCols[3].classList.add(fbArray[i+3]);
rowCols[3].textContent = fbArray[i+3];
rowCols[4].classList.add(fbArray[i+4]);
rowCols[4].textContent = fbArray[i+4];
tableBody.appendChild(tableRow);
}
}
</code>
</pre>
</div>
<div class="col-lg-4">
<The>The Code is structured in <strong>three</strong> functions. <br> There are also <strong>three</strong> different solutions provided as I had fun solving this problem!</p>
<h5>getValues</h5>
<p>The <strong>getValues</strong> function gets the value for fizz and buzz from the app page. The function utilizes the <strong>getElementById</strong> to pull the values
from the page. We make sure the numbers for fizz and buzz are Integers, and if they are, we pass the values <strong>fizzValue</strong> and <strong>buzzValue</strong> to the <strong>fizzBuzz</strong> function.
The function <strong>fizzBuzz</strong> then returns an array that we set to the value <strong>fbArray</strong> before we pass that
same value to the <strong>displayValues</strong> function. For this solution, we have chosen to select <strong>fizzBuzzC</strong> as our logic function to solve this problem!</p>
<h5>fizzBuzzA (For-Loop Solution)</h5>
<p>This <strong>fizzBuzz</strong> function is the <strong>For-Loop Solution</strong>. It takes in two parameters (<strong>fizzValue</strong> and <strong>fizzValue</strong>).
We initalize an empty array to <strong>returnArray</strong>. We then use a for loop to go through numbers 1 to 100, storing a value into the array for each number.
Using the modulus operator, if the number is divisible by <strong>fizzValue</strong> and <strong>buzzValue</strong>, we push in the word FizzBuzz. Else, if the number
is not divisible by both fizz and buzz values, but only by <strong>fizzValue</strong>, we push in the word Fizz. Else, if the number
is not divisible by both fizz and buzz values, or the <strong>fizzValue</strong>, but is divisible by <strong>buzzValue</strong> we push in the word Buzz.
If none of these previous options are valid, then we send in the number at index 'i' to the array, <strong>returnArray</strong>.</p>
<h5>fizzBuzzB (Switch Statement Solution)</h5>
<p>This <strong>fizzBuzz</strong> function is the <strong>Switch Statement Solution</strong>. It takes in two parameters (<strong>fizzValue</strong> and <strong>fizzValue</strong>).
We initalize an empty array to <strong>returnArray</strong>, and set boolean values <strong>Fizz</strong> and <strong>Buzz</strong> to <strong>false</strong>.
We then use a for loop to go through numbers 1 to 100, and set statements for when <strong>Fizz</strong> and <strong>Buzz</strong> are divisible that they would be <strong>true</strong>.
Using a switch statement, in the case where <strong>Fizz</strong> and <strong>Buzz</strong> are <strong>true</strong>, we push in the word FizzBuzz to the array at that index.
In the case where <strong>Fizz</strong> is only <strong>true</strong>, we push in the word Fizz to the array at that index.
In the case where <strong>Buzz</strong> is only <strong>true</strong>, we push in the word Buzz to the array at that index.
The default case is just to push the number value of the index into the array <strong>returnArray</strong> if the previous conditions were not met.</p>
<h5>fizzBuzzC (Ternary Operator Solution)</h5>
<p>This <strong>fizzBuzz</strong> function is the <strong>Ternary Operator Solution</strong>. It takes in two parameters (<strong>fizzValue</strong> and <strong>fizzValue</strong>).
We initalize an empty array to <strong>returnArray</strong>.
We then use a for loop to go through numbers 1 to 100, and inside the loop we use a ternary operator for when the <strong>fizzValue</strong> is divisble by 'i', that we add the
word Fizz to the array, additionally, we use a ternary operator for when the <strong>buzzValue</strong> is divisble by 'i', that we add the word Buzz to the array, OR we add the
value 'i' to the array. This helps us cover the case where we need to add the word FizzBuzz to the array, as we concatenate the word Fizz with Buzz when both are divisible by 'i'.
The array <strong>returnArray</strong> is then returned.</p>
<h5>displayValues</h5>
<p>The function <strong>displayValues</strong> takes in the parameter <strong>fbArray</strong> (the variable is an array that now holds the list of FizzBuzz values from 1 to 100).
We then get <strong>tableBody</strong> and <strong>templateRow</strong> from our HTML. We then clear the table before adding values.
Then we create a for loop that increments 5 values at a time and also create values <strong>tableRow</strong> and <strong>rowCols</strong> to grab all the td tags in our HTML.
Then at each of the 5 indices at a time for each of the 5 rows, we add the class element from the CSS to provide the colour for our words in our display, while also adding the content
into the td tags. We then append all 5 values into our rows inside of our <strong>template element</strong> inside our HTML where the final display is shown to the user!</p>
</div>
</div>
</div>
</main>
<!-- Footer Section -->
<footer class="footer mt-auto py-3 sticky-footer">
<div class="container-fluid">
<div class="row row-cols-1 row-cols-lg-3 gy-2">
<div class="col d-flex order-last order-lg-first text-light align-items-center justify-content-center">
<div class="info-content"><span class="text-muted">©2023</span> <a target="_blank" href="https://julianmacioce.me/" style="text-decoration:none">Julian Macioce</a> | <a href="mailto:macioce@uwindsor.ca" style="text-decoration:none">macioce@uwindsor.ca</a></div>
</div>
<div class="col d-flex align-items-center justify-content-center justify-content-lg-center brand">
<a target="_blank" href="https://julianmacioce.me/"><img src="img/brandlogo.png" alt="Julian Macioce Logo" height="36" width="100" title="Visit My Website!"></a>
</div>
<div class="col d-flex align-items-center justify-content-center justify-content-lg-end">
<div class="row">
<div class="col social"><a href="https://www.linkedin.com/in/julianmacioce/" target="_blank"><i class="fab fa-linkedin fa-2x"></i></a></div>
<div class="col social"><a href="https://github.com/JMacioce" target="_blank"><i class="fab fa-github fa-2x"></i></a></div>
<!-- <div class="col social"><a href="#" target="_blank"><i class="fab fa-youtube fa-2x"></i></a></div> -->
</div>
</div>
</div>
</div>
</footer>
<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous">
</script>
<script src="js/prism.js"></script>
<script>
Prism.plugins.NormalizeWhitespace.setDefaults({
'remove-trailing': true,
'remove-indent': true,
'left-trim': true,
'right-trim': true
})
</script>
</body>
</html>