Skip to content

Commit 00e5a75

Browse files
author
Chris Weber
committed
my head is exploding
1 parent aa7ac24 commit 00e5a75

File tree

1 file changed

+159
-59
lines changed

1 file changed

+159
-59
lines changed

src/functions-and-arrays.js

Lines changed: 159 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,219 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
4-
2+
function maxOfTwoNumbers(num1, num2) {
3+
if (num1 > num2) {
4+
return num1;
5+
} else if (num1 < num2) {
6+
return num2;
7+
} else return num1;
8+
}
59

610
// Iteration #2: Find longest word
7-
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
8-
9-
function findLongestWord() {}
11+
const words = [
12+
"mystery",
13+
"brother",
14+
"aviator",
15+
"crocodile",
16+
"pearl",
17+
"orchard",
18+
"crackpot",
19+
];
1020

21+
function findLongestWord(arr) {
22+
if (arr.length === 0) {
23+
return null;
24+
} else {
25+
let words = " ";
26+
for (let i = 0; i < arr.length; i++) {
27+
if (words.length < arr[i].length) {
28+
words = arr[i];
29+
}
30+
}
31+
return words;
32+
}
33+
}
1134

1235

1336
// Iteration #3: Calculate the sum
1437
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
1538

16-
function sumNumbers() {}
39+
function sumNumbers(arr) {
40+
let sum = 0;
41+
42+
if (arr.length === 0) {
43+
return 0;
44+
} else {
45+
arr.forEach((num) => {
46+
sum += num;
47+
});
48+
return sum;
49+
}
50+
}
1751

1852

53+
///////////////////////////////////////////////////////////////////////
1954

2055
// Iteration #3.1 Bonus:
2156
function sum() {}
22-
57+
///////////////////////////////////////////////////////////////////////
2358

2459

2560
// Iteration #4: Calculate the average
2661
// Level 1: Array of numbers
2762
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2863

29-
function averageNumbers() {}
64+
function averageNumbers(arr) {
65+
let sum = 0;
66+
if (arr.length === 0) {
67+
return null;}
68+
else {
69+
for ( i = 0; i < arr.length; i++) {
70+
sum += arr[i];
71+
}
72+
return sum / arr.length;
73+
}
74+
}
75+
76+
3077

3178

3279
// Level 2: Array of strings
33-
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
80+
const wordsArr = [
81+
"seat",
82+
"correspond",
83+
"linen",
84+
"motif",
85+
"hole",
86+
"smell",
87+
"smart",
88+
"chaos",
89+
"fuel",
90+
"palace",
91+
];
3492

35-
function averageWordLength() { }
93+
function averageWordLength() {}
3694

3795
// Bonus - Iteration #4.1
3896
function avg() {}
3997

4098
// Iteration #5: Unique arrays
4199
const wordsUnique = [
42-
'crab',
43-
'poison',
44-
'contagious',
45-
'simple',
46-
'bring',
47-
'sharp',
48-
'playground',
49-
'poison',
50-
'communion',
51-
'simple',
52-
'bring'
100+
"crab",
101+
"poison",
102+
"contagious",
103+
"simple",
104+
"bring",
105+
"sharp",
106+
"playground",
107+
"poison",
108+
"communion",
109+
"simple",
110+
"bring",
53111
];
54112

55113
function uniquifyArray() {}
56114

57-
58-
59115
// Iteration #6: Find elements
60-
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
116+
const wordsFind = [
117+
"machine",
118+
"subset",
119+
"trouble",
120+
"starting",
121+
"matter",
122+
"eating",
123+
"truth",
124+
"disobedience",
125+
];
61126

62127
function doesWordExist() {}
63128

64-
65-
66129
// Iteration #7: Count repetition
67130
const wordsCount = [
68-
'machine',
69-
'matter',
70-
'subset',
71-
'trouble',
72-
'starting',
73-
'matter',
74-
'eating',
75-
'matter',
76-
'truth',
77-
'disobedience',
78-
'matter'
131+
"machine",
132+
"matter",
133+
"subset",
134+
"trouble",
135+
"starting",
136+
"matter",
137+
"eating",
138+
"matter",
139+
"truth",
140+
"disobedience",
141+
"matter",
79142
];
80143

81144
function howManyTimes() {}
82145

83-
84-
85146
// Iteration #8: Bonus
86147
const matrix = [
87148
[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
88-
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],
89-
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],
149+
[
150+
49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62,
151+
0,
152+
],
153+
[
154+
81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36,
155+
65,
156+
],
90157
[52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91],
91-
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
92-
[24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
93-
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
94-
[67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21],
95-
[24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
158+
[
159+
22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13,
160+
80,
161+
],
162+
[
163+
24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12,
164+
50,
165+
],
166+
[
167+
32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64,
168+
70,
169+
],
170+
[
171+
67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94,
172+
21,
173+
],
174+
[
175+
24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63,
176+
72,
177+
],
96178
[21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95],
97179
[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92],
98-
[16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57],
180+
[
181+
16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85,
182+
57,
183+
],
99184
[86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
100-
[19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40],
185+
[
186+
19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55,
187+
40,
188+
],
101189
[4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
102-
[88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
103-
[4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36],
104-
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
105-
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
106-
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
190+
[
191+
88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53,
192+
69,
193+
],
194+
[
195+
4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76,
196+
36,
197+
],
198+
[
199+
20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36,
200+
16,
201+
],
202+
[
203+
20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5,
204+
54,
205+
],
206+
[
207+
1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67,
208+
48,
209+
],
107210
];
108211

109212
function greatestProduct() {}
110213

111-
112-
113-
114214
// The following is required to make unit tests work.
115215
/* Environment setup. Do not modify the below code. */
116-
if (typeof module !== 'undefined') {
216+
if (typeof module !== "undefined") {
117217
module.exports = {
118218
maxOfTwoNumbers,
119219
findLongestWord,
@@ -125,6 +225,6 @@ if (typeof module !== 'undefined') {
125225
uniquifyArray,
126226
doesWordExist,
127227
howManyTimes,
128-
greatestProduct
228+
greatestProduct,
129229
};
130230
}

0 commit comments

Comments
 (0)