Skip to content

Commit f351e40

Browse files
committed
1 parent bbd5108 commit f351e40

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
FUNCTIONS AND ARRAYS LAB
11+
</body>
12+
<script src="src/functions-and-arrays.js"></script>
13+
14+
</html>

src/functions-and-arrays.js

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,88 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
2+
// Implement the function maxOfTwoNumbers that takes two numbers as arguments and returns the largest.
3+
function maxOfTwoNumbers(num1, num2) {
4+
if(num1>num2) {
5+
return num1
6+
}else {
7+
return num2
8+
}
9+
}
410

11+
console.log(maxOfTwoNumbers(1, 2))
512

613
// Iteration #2: Find longest word
714
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
15+
function findLongestWord(array) {
16+
let longstr = '';
17+
for(let i = 0; i < array.length; i++ ) {
18+
if(array[i].length > longstr,length) {
19+
longstr = array[i]
20+
}
21+
}
22+
return longest;
23+
}
24+
825

9-
function findLongestWord() {}
1026

1127

1228

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

16-
function sumNumbers() {}
32+
function sumNumbers(array) {
33+
let sum = 0;
34+
for (let i = 0; i<array.length; i++) {
35+
sum += array[i];
36+
}
37+
return sum;
38+
}
39+
40+
1741

1842

1943

2044
// Iteration #3.1 Bonus:
21-
function sum() {}
45+
46+
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
47+
48+
function sum(mixArr) {
49+
let total = 0;
50+
for (let i=0; i < mixArr.length; i++) {
51+
if (typeof mixArr[i] === "number") {
52+
total +- mixArr[i]
53+
} else if (typeof mixArr[i] === "string") {
54+
total +- mixArr[i].length
55+
} else if (typeof mixArr[i] === "boolean")
56+
if (mixArr[i] === true) {
57+
58+
} else {
59+
continue
60+
}
61+
}
62+
return total
63+
}
64+
65+
66+
67+
2268

2369

2470

2571
// Iteration #4: Calculate the average
2672
// Level 1: Array of numbers
2773
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2874

29-
function averageNumbers() {}
75+
function averageNumbers(numbers) {
76+
let sum=0;
77+
if (numbers.length === 0) {
78+
return null;
79+
}
80+
for (let i=0; i < numbers.length; i++) {
81+
sum += numbers[i];
82+
}
83+
return sum / numbers.length;
84+
}
85+
3086

3187

3288
// Level 2: Array of strings
@@ -53,7 +109,10 @@ const wordsUnique = [
53109
];
54110

55111
function uniquifyArray() {}
112+
let cleanArray
113+
56114

115+
57116

58117

59118
// Iteration #6: Find elements

0 commit comments

Comments
 (0)