Skip to content

Commit 3df38cf

Browse files
committed
Solve issues until Iteration ironhack-labs#6: Find elements
1 parent 0f94b74 commit 3df38cf

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

src/functions-and-arrays.js

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,80 @@
11
// Iteration #1: Find the maximum
22

3+
function maxOfTwoNumbers(number1, number2) {
4+
if (number1 > number2) {
5+
return number1
6+
} else {
7+
return number2
8+
}
9+
}
10+
311
// Iteration #2: Find longest word
412
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
513

14+
function findLongestWord(arr) {
15+
if (arr.length == 0) {
16+
return null
17+
}
18+
let longest = ""
19+
for (let i = 0; i <= arr.length - 1; i++) {
20+
if (longest.length < arr[i].length) {
21+
longest = arr[i];
22+
}
23+
}
24+
return longest
25+
}
26+
27+
628
// Iteration #3: Calculate the sum
729

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

32+
function sumNumbers(arr) {
33+
if (arr == "") {
34+
return 0
35+
}
36+
let total = 0
37+
for (let i = 0; i < arr.length; i++) {
38+
total += Number(arr[i]);
39+
}
40+
return total
41+
}
42+
1043
// Iteration #4: Calculate the average
44+
1145
// Level 1: Array of numbers
1246
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
1347

48+
function averageNumbers(arr) {
49+
if (arr == "") {
50+
return null
51+
}
52+
let total = 0
53+
for (let i = 0; i < arr.length; i++) {
54+
total += Number(arr[i]);
55+
}
56+
let avg = total / arr.length;
57+
return avg
58+
}
59+
60+
61+
1462
// Level 2: Array of strings
1563
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
1664

65+
function averageWordLength(str) {
66+
if (str == "") {
67+
return null
68+
}
69+
let total = 0
70+
for (let i = 0; i < str.length; i++) {
71+
total += Number(str[i].length);
72+
}
73+
let avg = total / str.length;
74+
return avg
75+
}
76+
77+
1778
// Iteration #5: Unique arrays
1879
const wordsUnique = [
1980
'crab',
@@ -29,9 +90,31 @@ const wordsUnique = [
2990
'bring'
3091
];
3192

93+
function uniquifyArray(arr) {
94+
if (arr == "") {
95+
return null
96+
}
97+
let i,
98+
len = arr.length,
99+
out = [],
100+
obj = {};
101+
102+
for (i = 0; i < len; i++) {
103+
obj[arr[i]] = 0;
104+
}
105+
for (i in obj) {
106+
out.push(i);
107+
}
108+
return out;
109+
}
110+
111+
console.log(uniquifyArray(words))
112+
32113
// Iteration #6: Find elements
33114
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
34115

116+
117+
35118
// Iteration #7: Count repetition
36119
const wordsCount = [
37120
'machine',
@@ -70,4 +153,4 @@ const matrix = [
70153
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
71154
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
72155
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
73-
];
156+
];

0 commit comments

Comments
 (0)