Skip to content

Commit 1f2f749

Browse files
committed
commit message
1 parent f86c5b4 commit 1f2f749

File tree

1 file changed

+29
-38
lines changed

1 file changed

+29
-38
lines changed

src/functions-and-arrays.js

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,8 @@ function maxOfTwoNumbers(firstNumber, secondNumber) {
1212
console.log(maxOfTwoNumbers(3, 3))
1313

1414
// Iteration #2: Find longest word
15-
/* const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
16-
function findLongestWord(array) {
17-
let base = '';
18-
for (let i in array) {
19-
if (base.length < array[i].length) {
20-
base = array[i]
21-
}
22-
}
23-
return base;
24-
}
25-
*/
2615
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
16+
//case Expected '' to be null. doesn't work
2717
function findLongestWord(array) {
2818
let base = '';
2919
if (array !== []) {
@@ -32,15 +22,12 @@ function findLongestWord(array) {
3222
base = array[i]
3323
}
3424
}
35-
return base;
3625
} else {
37-
return null;
26+
base = 0;
3827
}
39-
40-
}
41-
42-
43-
console.log(findLongestWord(words))
28+
return base;
29+
}
30+
console.log(findLongestWord(words))
4431

4532
// Iteration #3: Calculate the sum
4633

@@ -73,41 +60,45 @@ console.log(sum(mixedArr))
7360

7461
// Iteration #4: Calculate the average
7562
// Level 1: Array of numbers
63+
64+
//case "Calculate the average of an array of numbers should return null if receives an empty array when called" doesnt work
7665
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
7766
function averageNumbers (numArrayAver) {
7867
let sumForAverage = 0;
79-
for (let z = 0; z < numArrayAver.length; z++) {
80-
sumForAverage += numArrayAver[z]
81-
}
82-
if(numArrayAver.length > 1) {
83-
let result = (sumForAverage / (numArrayAver.length - 1)).toFixed(0)
84-
return result;
68+
if (numArrayAver.length === 1) {
69+
sumForAverage = numArrayAver[0];
8570
} else {
86-
return sumForAverage
71+
for (let z = 0; z < numArrayAver.length; z++) {
72+
sumForAverage += numArrayAver[z];
73+
}
74+
sumForAverage /= numArrayAver.length;
8775
}
88-
76+
return Number(sumForAverage.toFixed(0))
8977
}
9078
console.log(averageNumbers(numbersAvg));
9179

9280
// Level 2: Array of strings
81+
82+
//case item.length === 0 doesn't workf
9383
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
9484
function averageWordLength(item) {
95-
let sumLength = 0;
96-
for (let b = 0; b < item.length; b++) {
97-
if (item.length > 1) {
98-
sumLength += item[b].length;
99-
return sumLength
100-
} else if (item.length === 1) {
101-
return item[b].length;
102-
} else {
103-
return null;
104-
}
85+
if (item.length === 0) {
86+
return null;
87+
} else {
88+
let sumLength = 0;
89+
for (let b = 0; b < item.length; b++) {
90+
if (item.length > 1) {
91+
sumLength += item[b].length;
92+
93+
} else if (item.length === 1) {
94+
return item[0].length;
95+
}
96+
} return sumLength / item.length
10597
}
106-
10798
}
108-
10999
console.log(averageWordLength(wordsArr))
110100

101+
111102
// Iteration #5: Unique arrays
112103
const wordsUnique = [
113104
'crab',

0 commit comments

Comments
 (0)