forked from SEI-NYC-1114/Control_Flow_Homework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge.js
387 lines (318 loc) Β· 8.15 KB
/
challenge.js
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
////////////////////////////////////
// READ THE INSTRUCTIONS BELOW BEFORE STARTING!
////////////////////////////////////
/*
* Before you start, add this javascript file in a script tag in index.html.
*
* The loops are wrapped in functions for testing purposes. Please do not modify the names of the functions.
*
* Test your output in the browser by commenting in and out the function call (aka invocation), e.g., "prompt1()"
*
*/
/*
* Prompt 1:
*
* Create a loop that counts from 0 to 10, printing each number (including 0 and 10).
*/
// The first one has been done for you as an example!
function prompt1() {
for (let i = 0; i <= 10; i++) {
console.log(i);
}
}
console.log.prompt1();
// COMMENT IN THE BELOW LINE OF CODE TO TEST YOUR OUTPUT IN THE BROWSER!
// prompt1()
// π MAKE A COMMIT: "Complete prompt 1"
/*
* Prompt 2:
*
* Create a loop that prints every even number BETWEEN 0 and 100 (not including 0 or 100).
*/
function prompt2() {
for (let i = 2; i <= 98; i+=2) {
console.log(i);
}
}
//Other way to do it//
function prompt2() {
for (let i = 2; i <= 100; i++) {
if(!(i % 2)) {
console.log(i);
}
}
console.log.prompt2();
// COMMENT IN THE BELOW LINE OF CODE TO TEST YOUR OUTPUT IN THE BROWSER!
// prompt2();
// π MAKE A COMMIT: "Complete prompt 2"
/*
* Prompt 3:
*
* Create a loop that counts from -5 to 5, printing each number (including -5 and 5).
*/
function prompt3() {
for(let i = -5; i <= 5; i++) {
console.log(i)
}
}
console.log.prompt3()
// COMMENT IN THE BELOW LINE OF CODE TO TEST YOUR OUTPUT IN THE BROWSER!
// prompt3()
// π MAKE A COMMIT: "Complete prompt 3"
/*
* Prompt 4:
*
* Create a loop that counts down from 10 to 0, printing each number (including 10 and 0).
*/
function prompt4() {
for(let i = 10; i >= 0; i--){
consolge.log(i);
}
}
console.log.prompt4()
// COMMENT IN THE BELOW LINE OF CODE TO TEST YOUR OUTPUT IN THE BROWSER!
// prompt4()
// π MAKE A COMMIT: "Complete prompt 4"
/*
* Prompt 5:
*
* Create a loop that counts down from 5 to -5, printing each number (including 5 and -5).
*/
function prompt5() {
for(let i = 5; i <= -5; i--){
console.log(i)
}
}
console.log.prompt5()
// COMMENT IN THE BELOW LINE OF CODE TO TEST YOUR OUTPUT IN THE BROWSER!
// prompt5()
// π MAKE A COMMIT: "Complete prompt 5"
/*
* Prompt 6:
*
* Create a loop that counts from 0 to 50 (inclusive) in multiples of 2 (instead of 1),
* printing each number.
*/
function prompt6() {
for(let i = 0; i <= 50; i+=2){
console.log(i)
}
}
console.log.prompt6()
// COMMENT IN THE BELOW LINE OF CODE TO TEST YOUR OUTPUT IN THE BROWSER!
// prompt6()
// π MAKE A COMMIT: "Complete prompt 6"
/*
* Prompt 7:
*
* For the numbers 0 - 100 (inclusive), print out "I found a [ number ]. High five!" if the
* number is a multiple of five.
*
* Sample Output IN THE BROWSER:
* - I found a 5. High five!
* - I found a 10. High five!
*/
function prompt7() {
for(let i = 0; i <= 100; i++) {
if(i % 5 ==0){
console.log('I found a ${i}. high five!')
}
}
}
console.log.prompt7()
// COMMENT IN THE BELOW LINE OF CODE TO TEST YOUR OUTPUT IN THE BROWSER!
// prompt7()
// π MAKE A COMMIT: "Complete prompt 7"
/*
* Prompt 8:
*
* Create a variable called someNumber and assign it a random number between
* 0 and 100 (not inclusive). Hint: Use Math.random so that it generates a new number every time the code is run!
*
* Create a conditional that matches these requirements:
* - if someNumber is less than 30, print "That's a small number!"
* - if someNumber is between 30 and 60, print "The number is medium sized."
* - if someNumber is greater than 60, print "We got a big one!"
*/
// define someNumber here
let someNumber
function prompt8() {
someNumber = Math.floor(Math.random()* 99) + 1
console.log(someNumber);
if (someNumber >= 1 && someNumber <=30) {
console.log("That's a small number!")
} else if (someNumber > 30 && someNumber <=60){
}
for(i )
}
console.log.prompt8();
// COMMENT IN THE BELOW LINE OF CODE TO TEST YOUR OUTPUT IN THE BROWSER!
// prompt8();
// π MAKE A COMMIT: "Complete prompt 8"
/*
* Prompt 9:
*
* Iterate over the starWars array printing each character's name and index
*
* i.e.:
* 0. Han
* 1. C3PO
* 2. R2D2
*/
const starWars = ["Han", "C3PO", "R2D2", "Luke", "Leia", "Anakin"];
function prompt9() {
for (let i = 0; i < starWars.length; i++)
console.log('${[i]}. ${starWars[i]}');
}
console.log.prompt9();
// COMMENT IN THE BELOW LINE OF CODE TO TEST YOUR OUTPUT IN THE BROWSER!
// prompt9();
// π MAKE A COMMIT: "Complete prompt 9"
/*
* Prompt 10:
*
* Write a loop that pushes every even number between 0 and 100 into an array,
* then print the array.
*/
function prompt10() {
const myArray = [];
for(let i = 1; i < 100; i++)
if(i % 2 == 0)
myArray.push(i);
// don't forget to return the array after pushing the numbers into it so you can see it in the browser!
// return the array
return myArray;
}
console.log(prompt10());
// COMMENT IN THE BELOW LINE OF CODE TO TEST YOUR OUTPUT IN THE BROWSER!
// console.log(prompt10());
// π MAKE A COMMIT: "Complete prompt 10"
/*
* Prompt 11:
*
* Find the median number in the following nums array (the mid point), then
* console.log that number. The median number is the middle number in a SORTED list of numbers. Think through the steps you'll need to take to determine this number with code!
*
* Hint: What does Math.floor() do?
*/
let nums = [
14,
11,
16,
15,
13,
16,
15,
17,
19,
11,
12,
14,
19,
11,
15,
17,
11,
18,
12,
17,
12,
71,
18,
15,
12,
];
function prompt11(arr) {
let median;
arguments.sort((a, b)) => {
return a - b;
}
console.log(arr);
const middle = Math.floor(arr.length / 2);
return arr[middle]
}
console.log(prompt11(nums));
// COMMENT IN THE BELOW LINE OF CODE TO TEST YOUR OUTPUT IN THE BROWSER!
// π MAKE A COMMIT: "Complete prompt 11"
/*
* Prompt 12:
*
* Did you know that you can nest loops? The catch is that you have to use
* different incrementers (normally i) in the inner vs. outer loops. You'll commonly see developers use
* another 1-letter incrementer (j, k, l, etc).
*
* Create an "outer" loop that counts up from 1 to 10 with an incrementer of i.
* Create an "inner" loop that counts from 11 to 20 with an incrementer of j.
* Inside the inner loop, print `i: ${i} / j: ${j}`
* You might need to consult Google to see examples of nested for loops!
*/
function prompt12() {
for(let i =1; i <= 10; i++){
for(let j = 11; j <= 20; j++){
console.log('i: $i / j: ${j}')
}
}
}
const prompt12 = funtion {
for(let i = 1; i < 11; i++){
for(let j = 11; j < 21; j++) {
console.log('i: ${i} / j: ${j}')
}
}
}
console.log.prompt12();
// COMMENT IN THE BELOW LINE OF CODE TO TEST YOUR OUTPUT IN THE BROWSER!
// prompt12();
// π MAKE A COMMIT: "Complete prompt 12"
/*
* Prompt 13:
*
* Using nested loops, print all the values inside the nested arrays.
*/
let nestedArrays = [
["a", "b", "c"],
["d", "e", "f"],
["g", "h", "i"],
];
function prompt13() {
// YOUR CODE HERE
}
// COMMENT IN THE BELOW LINE OF CODE TO TEST YOUR OUTPUT IN THE BROWSER!
// prompt13();
// π MAKE A COMMIT: "Complete prompt 13"
///////////////////////////////////////////
// DO NOT MODIFY CODE BENEATH THIS LINE :)
///////////////////////////////////////////
if (!this.window) {
module.exports = {
prompt1,
prompt2,
prompt3,
prompt4,
prompt5,
prompt6,
prompt7,
someNumber,
prompt8,
prompt9,
prompt10,
prompt11,
prompt12,
prompt13,
};
}
//Class Exercise//
//function computerArea(width, height) {
// let area = width * height;
// return 'The area of rectangle with width of ${width} and height of ${height} is ${area} square units'
//}
//console.log(computerArea(5, 25));
//function planetHasWater(planet) {
// if(planet.toUpperCase() === 'EARTH' || planet.toUpperCase() === 'MARS')
// return true;
// else return false;
}
//console.log(planetHasWater('Earth') ) //=> true
//console.log(planetHasWater('Venus') ) //=> false
// Test the bonus...
//console.log(planetHasWater('mArS') ) //=> true