Skip to content

Commit 132bee6

Browse files
committed
finish iteration 1 to 3, but I can't do ironhack-labs#2 last request
1 parent 4cb03c7 commit 132bee6

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

src/functions-and-arrays.js

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,63 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
2+
function maxOfTwoNumbers(a,b) {
3+
if(a>b) {
4+
return a
5+
} else {
6+
return b
7+
}
8+
}
39

410

511

612
// Iteration #2: Find longest word
7-
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
13+
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpots'];
14+
15+
function findLongestWord(arr) {
16+
let longestWordArr = [];
17+
if (arr.length === 0){
18+
return null;
19+
}
20+
21+
if (arr.length === 1){
22+
return arr[0];
23+
}
24+
25+
let longestWord;
26+
for (i=0; i<(arr.length -1); i++){
27+
if (arr[i].length >= arr[i+1].length){
28+
longestWord=arr[i];
29+
}
30+
}
31+
32+
return longestWord;
33+
/*
34+
for (j=0; j<arr.length; j++){
35+
if (arr[j].length === longestWord.length) {
36+
longestWordArr.push(arr[j]);
37+
}
38+
}
39+
40+
return longestWordArr;*/
41+
}
842

9-
function findLongestWord() {}
43+
//console.log(findLongestWord(words));
1044

1145

1246

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

16-
function sumNumbers() {}
50+
function sumNumbers(arr) {
51+
let sum=0;
52+
53+
arr.forEach(number => {
54+
sum += number;
55+
});
56+
return sum;
57+
}
58+
59+
let result = sumNumbers(numbers);
60+
console.log(result);
1761

1862

1963

0 commit comments

Comments
 (0)