Skip to content

Commit 0c7580e

Browse files
committed
Init import
1 parent 0f94b74 commit 0c7580e

File tree

1 file changed

+175
-1
lines changed

1 file changed

+175
-1
lines changed

src/functions-and-arrays.js

Lines changed: 175 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,112 @@
11
// Iteration #1: Find the maximum
2+
function maxOfTwoNumbers(num1, num2) {
3+
if (num1 > num2) {
4+
return num1
5+
} else {
6+
return num2
7+
}
8+
}
29

310
// Iteration #2: Find longest word
411
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
512

13+
function findLongestWord(arrayWords) {
14+
let wordMaxLength = ''
15+
16+
if (arrayWords.length === 0) return null
17+
18+
for (let i = 0; i < arrayWords.length; i++) {
19+
if (wordMaxLength.length < arrayWords[i].length) {
20+
wordMaxLength = arrayWords[i]
21+
}
22+
}
23+
24+
return wordMaxLength
25+
}
26+
27+
findLongestWord(words)
28+
629
// Iteration #3: Calculate the sum
730

831
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
932

33+
function sumNumbers(arrayNumbers) {
34+
if (arrayNumbers.length === 0) return 0
35+
36+
let sumNums = 0
37+
for (let i = 0; i < arrayNumbers.length; i++) {
38+
sumNums += arrayNumbers[i]
39+
}
40+
return sumNums
41+
}
42+
43+
sumNumbers(numbers)
44+
45+
//Bonus - Iteration #3.1
46+
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
47+
48+
function sum(itemsArray) {
49+
if (itemsArray.length === 0) return 0
50+
51+
let sumItems = 0
52+
for (let i = 0; i < itemsArray.length; i++) {
53+
if (typeof itemsArray[i] === "number") {
54+
sumItems += itemsArray[i]
55+
} else if (typeof itemsArray[i] === "string") {
56+
sumItems += itemsArray[i].length
57+
} else if (typeof itemsArray[i] === "boolean") {
58+
sumItems += 1
59+
} else if (typeof itemsArray[i] === "object") {
60+
return "Error"
61+
} else if (typeof itemsArray[i] === "array") {
62+
return "Error"
63+
}
64+
}
65+
return sumItems
66+
}
67+
68+
sum(mixedArr)
69+
1070
// Iteration #4: Calculate the average
1171
// Level 1: Array of numbers
12-
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
72+
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]
73+
74+
function averageNumbers(arrayNumbers) {
75+
if (arrayNumbers.length === 0) return 0
76+
77+
const average = sum(arrayNumbers) / arrayNumbers.length
78+
return average
79+
}
80+
81+
averageNumbers(numbersAvg)
82+
1383

1484
// Level 2: Array of strings
1585
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
1686

87+
function averageWordLength(arrayWords) {
88+
if (arrayWords.length === 0) return null
89+
90+
const wordToNumberArray = []
91+
for (i = 0; i < arrayWords.length; i++) {
92+
wordToNumberArray.push(arrayWords[i].length)
93+
}
94+
return averageNumbers(wordToNumberArray)
95+
}
96+
97+
averageWordLength(wordsArr)
98+
99+
// Bonus - Iteration #4.1
100+
const anotherMixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]
101+
102+
function avg(itemsArray) {
103+
if (itemsArray.length === 0) return null
104+
return sum(itemsArray) / itemsArray.length
105+
}
106+
107+
avg(anotherMixedArr)
108+
109+
17110
// Iteration #5: Unique arrays
18111
const wordsUnique = [
19112
'crab',
@@ -29,9 +122,37 @@ const wordsUnique = [
29122
'bring'
30123
];
31124

125+
function uniquifyArray(arr) {
126+
if (arr.length === 0) return null
127+
128+
for (i = 0; i < arr.length; i++) {
129+
for (j = ++i; j < arr.length; j++) {
130+
if (arr[i] === arr[j]) {
131+
arr.splice(j, 1);
132+
}
133+
}
134+
}
135+
return arr
136+
}
137+
138+
uniquifyArray(wordsUnique)
139+
32140
// Iteration #6: Find elements
33141
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
34142

143+
144+
function doesWordExist(arr, word) {
145+
if (arr.length === 0) return null
146+
147+
for (i = 0; i < arr.length; i++) {
148+
if (arr.includes(word)) {
149+
return true
150+
}
151+
return false
152+
}
153+
154+
}
155+
35156
// Iteration #7: Count repetition
36157
const wordsCount = [
37158
'machine',
@@ -47,6 +168,18 @@ const wordsCount = [
47168
'matter'
48169
];
49170

171+
function howManyTimes(arr, word) {
172+
if (arr.length === 0) return null
173+
let wordTimes = 0
174+
for (i = 0; i < arr.length; i++) {
175+
if (arr[i] === word) {
176+
wordTimes++
177+
}
178+
}
179+
return wordTimes
180+
}
181+
182+
howManyTimes(wordsCount, "matter")
50183
// Iteration #8: Bonus
51184

52185
const matrix = [
@@ -71,3 +204,44 @@ const matrix = [
71204
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
72205
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
73206
];
207+
208+
function greatestProduct(arr) {
209+
let multipliedNumbers = 0
210+
let multiplyResult
211+
for (i = 0; i < arr.length - 1; i++) {
212+
for (j = 0; j < arr[i].length - 1; j++) {
213+
if (i > 0 && j > 0) {
214+
multiplyResult = arr[i - 1][j] * arr[i + 1][j] * arr[i][j + 1] * arr[i][j - 1]
215+
//console.log(`${arr[i-1][j]} * ${arr[i+1][j]} * ${arr[i][j+1]} * ${arr[i][j-1]} = ${multiplyResult}`)
216+
if (multiplyResult > multipliedNumbers) {
217+
multipliedNumbers = multiplyResult
218+
//console.log(`multipliedNumbers = ${multipliedNumbers} / multiplyResult = ${multiplyResult}`)
219+
}
220+
}
221+
}
222+
}
223+
return multipliedNumbers
224+
}
225+
226+
greatestProduct(matrix)
227+
228+
function greatestProductOfDiagonals(arr) {
229+
if (arr.length === 0) return null
230+
let multipliedNumbers = 0
231+
let multiplyResult
232+
for (i = 0; i < arr.length - 1; i++) {
233+
for (j = 0; j < arr[i].length - 1; j++) {
234+
if (i > 0 && j > 0) {
235+
multiplyResult = arr[i - 1][j - 1] * arr[i - 1][j + 1] * arr[i + 1][j - 1] * arr[i + 1][j + 1]
236+
//console.log(`${arr[i - 1][j-1]} * ${arr[i - 1][j+1]} * ${arr[i + 1][j - 1]} * ${arr[i + 1][j + 1]} = ${multiplyResult}`)
237+
if (multiplyResult > multipliedNumbers) {
238+
multipliedNumbers = multiplyResult
239+
//console.log(`multipliedNumbers = ${multipliedNumbers} / multiplyResult = ${multiplyResult}`)
240+
}
241+
}
242+
}
243+
}
244+
return multipliedNumbers
245+
}
246+
247+
greatestProductOfDiagonals(matrix)

0 commit comments

Comments
 (0)