Skip to content

Commit 35321c4

Browse files
Gustavo JordaoGustavo Jordao
authored andcommitted
Solutions for iterations ironhack-labs#1-4
1 parent bc3a7b8 commit 35321c4

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

starter-code/src/functions-and-arrays.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,62 @@
11
// Iteration #1: Find the maximum
2+
function maxOfTwoNumbers(num1, num2) {
3+
if (typeof num1 == "number" && typeof num2 == "number") {
4+
if (num1 > num2) {
5+
return num1;
6+
} else if (num2 > num1) {
7+
return num2;
8+
} else if (num1 === num2) {
9+
return num1;
10+
} else {
11+
console.log("This is not a valid operation!");
12+
}
13+
} else {
14+
console.log("This is not a valid type of input");
15+
};
16+
};
217

318
// Iteration #2: Find longest word
4-
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
19+
function findLongestWord(arr) {
20+
let longestWord = "";
21+
if (arr.length === 0) {
22+
return null;
23+
} else if (arr.length > 0) {
24+
for (let i = 0; i < arr.length; i++) {
25+
if (longestWord.length < arr[i].length) {
26+
longestWord = arr[i];
27+
}
28+
}
29+
return longestWord;
30+
}
31+
};
532

633
// Iteration #3: Calculate the sum
7-
834
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
935

36+
function sumArray(arr) {
37+
let newArraySum = 0;
38+
for (let i = 0; i < arr.length; i++) {
39+
newArraySum += arr[i];
40+
}
41+
return newArraySum;
42+
};
43+
1044
// Iteration #4: Calculate the average
1145
// Level 1: Array of numbers
1246
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
1347

48+
function averageNumbers(arr) {
49+
let sumArr = 0;
50+
if (arr.length === 0) {
51+
return null;
52+
}
53+
for (let i = 0; i < arr.length; i++) {
54+
sumArr += arr[i];
55+
}
56+
let arrayAvg = sumArr / arr.length;
57+
return arrayAvg;
58+
};
59+
1460
// Level 2: Array of strings
1561
const wordsArr = [
1662
'seat',

0 commit comments

Comments
 (0)