Skip to content

Commit 3588def

Browse files
committed
Iteration up to ironhack-labs#5 completed
1 parent bbd5108 commit 3588def

File tree

1 file changed

+87
-10
lines changed

1 file changed

+87
-10
lines changed

src/functions-and-arrays.js

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,97 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
4-
2+
function maxOfTwoNumbers(a, b) {
3+
if (a > b){
4+
return a
5+
} else if (a < b){
6+
return b
7+
} else{
8+
return a
9+
}
10+
}
511

612
// Iteration #2: Find longest word
713
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
814

9-
function findLongestWord() {}
10-
15+
function findLongestWord(words) {
16+
//Check empty. return null
17+
let longestWord = "";
18+
if (words.length === 0){
19+
return null;
20+
} else if (words.length === 1) {
21+
return words[0]
22+
} else {
23+
for (i = 0; i < words.length; i++){
24+
25+
if (longestWord.length < words[i].length){
26+
longestWord = words[i]
27+
}
28+
}
29+
return longestWord;
30+
}
31+
//First iteration if same length
32+
}
33+
findLongestWord(words)
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(numbers) {
40+
41+
if (numbers.length === 0) {
42+
return 0
43+
} else if (numbers.length === 1) {
44+
return numbers[0]
45+
} else {
46+
let sum = 0
47+
for (i = 0; i < numbers.length; i++){
48+
sum += numbers[i]
49+
}
50+
return sum
51+
}
52+
}
1753

54+
// Iteration #3.1 Bonus:
55+
// const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
1856

57+
// // should return: 57
1958

20-
// Iteration #3.1 Bonus:
21-
function sum() {}
59+
function sum(mixedArr) {
60+
// if (number.length === 0) {
61+
// return 0
62+
// } else if (number.length === 1) {
63+
// return number[0]
64+
// } else {
65+
// let sum = 0
66+
// for (i = 0; i < number.length; i++){
67+
// sum += number[i]
68+
// }
69+
// return sum
70+
}
2271

2372

2473

2574
// Iteration #4: Calculate the average
2675
// Level 1: Array of numbers
2776
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2877

29-
function averageNumbers() {}
78+
function averageNumbers(numberArr) {
79+
let sum = 0
80+
let arrLength = numberArr.length
81+
if (numberArr.length === 0){
82+
return null
83+
} else if (numberArr.length === 1) {
84+
return numberArr[0]
85+
}
86+
else {
87+
for (i = 0; i < arrLength; i++){
88+
sum += numberArr[i]
89+
}
90+
return sum / arrLength
91+
}
92+
}
93+
94+
//console.log(averageNumbers(numbersAvg))
3095

3196

3297
// Level 2: Array of strings
@@ -52,9 +117,21 @@ const wordsUnique = [
52117
'bring'
53118
];
54119

55-
function uniquifyArray() {}
120+
function uniquifyArray(wordsUnique) {
121+
if (wordsUnique.length === 0){return null}
56122

123+
let collectWords = []
124+
for (i = 0; i < wordsUnique.length; i++){
125+
//console.log(collectWords.indexOf(wordsUnique[i]));
126+
if (collectWords.indexOf(wordsUnique[i]) === -1){
127+
collectWords.push(wordsUnique[i])
128+
//console.log(collectWords);
129+
}
130+
}
131+
return collectWords
132+
}
57133

134+
uniquifyArray(wordsUnique)
58135

59136
// Iteration #6: Find elements
60137
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];

0 commit comments

Comments
 (0)