Skip to content

Commit 1de7d1d

Browse files
committed
Complete until iteration ironhack-labs#5
1 parent bbd5108 commit 1de7d1d

File tree

1 file changed

+108
-19
lines changed

1 file changed

+108
-19
lines changed

src/functions-and-arrays.js

Lines changed: 108 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,112 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
4-
2+
function maxOfTwoNumbers(num1, num2) {
3+
// JS's Math.max own function calculates the highest among two numbers
4+
return Math.max(num1, num2);
5+
}
56

67
// Iteration #2: Find longest word
78
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
89

9-
function findLongestWord() {}
10-
11-
10+
function findLongestWord(arr) {
11+
// Check for empty array
12+
if (!arr.length) return null;
13+
14+
// Initialize variable to contain largest word found
15+
let largestWord = "";
16+
17+
// Loop to check every word in array. Largest will be stored in "largestWord"
18+
arr.forEach(element => {
19+
if (largestWord.length < element.length) {
20+
largestWord = element;
21+
}
22+
});
23+
return largestWord;
24+
}
1225

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

16-
function sumNumbers() {}
17-
18-
29+
function sumNumbers(arr) {
30+
let summ = 0;
31+
// Loop to add each number on array to summ
32+
for (item of arr) {
33+
summ += item;
34+
}
35+
return summ;
36+
}
1937

2038
// Iteration #3.1 Bonus:
21-
function sum() {}
22-
2339

40+
function sum(arr) {
41+
let summ = 0;
42+
// Loop goes through each number in array
43+
for (item of arr) {
44+
// Objects throw error message
45+
if (typeof item === 'object') {
46+
throw new Error("Unsupported data type sir or ma'am");
47+
}
48+
// Strings have their length added to summ
49+
else if (typeof item === 'string')
50+
{
51+
item = item.length;
52+
}
53+
// Numbers are just added
54+
else {
55+
item = Number(item);
56+
}
57+
summ += item;
58+
}
59+
return summ;
60+
}
2461

2562
// Iteration #4: Calculate the average
2663
// Level 1: Array of numbers
2764
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2865

29-
function averageNumbers() {}
66+
function averageNumbers(arr) {
67+
// Check for empty array
68+
if (arr.length === 0) {
69+
return null;
70+
}
71+
72+
// Invoke "sum" to have the summage of all elements
73+
summ = sum(arr);
74+
75+
// Average is the division of sum by the number of array's elements
76+
const avg = summ / arr.length;
77+
return avg;
78+
}
3079

3180

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

35-
function averageWordLength() { }
84+
function averageWordLength(arr) {
85+
// Check for empty array
86+
if (!arr.length) return null;
87+
88+
// Invoke function 'sum' which already adds values of string lenghts
89+
let summ = sum(arr);
90+
91+
// Average is the division of sum by the number of array's elements
92+
const avg = summ / arr.length;
93+
return avg;
94+
}
3695

3796
// Bonus - Iteration #4.1
38-
function avg() {}
97+
function avg(arr) {
98+
// Check for empty array
99+
if (arr.length === 0) {
100+
return null;
101+
}
102+
103+
// Invoke function 'sum' which already adds data types requested
104+
summ = sum(arr)
105+
106+
// Average is the division of sum by the number of array's elements, this time fixed to 2 decimal points
107+
const avg = Number((summ / arr.length).toFixed(2));
108+
return avg;
109+
}
39110

40111
// Iteration #5: Unique arrays
41112
const wordsUnique = [
@@ -52,14 +123,32 @@ const wordsUnique = [
52123
'bring'
53124
];
54125

55-
function uniquifyArray() {}
56-
57-
126+
function uniquifyArray(arr) {
127+
// Check for empty array
128+
if (!arr.length) return null;
129+
130+
let newArr = [];
131+
132+
for (let i = 0; i < arr.length; i++) {
133+
if (i === arr.indexOf(arr[i])) {
134+
newArr.push(arr[i]);
135+
}
136+
}
137+
return newArr;
138+
}
58139

59140
// Iteration #6: Find elements
60141
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61-
62-
function doesWordExist() {}
142+
word = 'subset';
143+
144+
function doesWordExist(arr, word) {
145+
for (let i = 0; i < arr.length; i++) {
146+
if (word === arr[i]) {
147+
return true;
148+
break;
149+
}
150+
}
151+
}
63152

64153

65154

0 commit comments

Comments
 (0)