Skip to content

Commit 2bc2ad9

Browse files
committed
ajustes
1 parent 0c7580e commit 2bc2ad9

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/functions-and-arrays.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ function sum(itemsArray) {
5454
sumItems += itemsArray[i]
5555
} else if (typeof itemsArray[i] === "string") {
5656
sumItems += itemsArray[i].length
57-
} else if (typeof itemsArray[i] === "boolean") {
57+
} else if (itemsArray[i] === true) {
5858
sumItems += 1
59+
} else if (itemsArray[i] === false) {
60+
sumItems += 0
5961
} else if (typeof itemsArray[i] === "object") {
60-
return "Error"
62+
throw new Error("Unsupported data type sir or ma'am")
6163
} else if (typeof itemsArray[i] === "array") {
62-
return "Error"
64+
throw new Error("Unsupported data type sir or ma'am")
6365
}
6466
}
6567
return sumItems
@@ -72,7 +74,7 @@ sum(mixedArr)
7274
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]
7375

7476
function averageNumbers(arrayNumbers) {
75-
if (arrayNumbers.length === 0) return 0
77+
if (arrayNumbers.length === 0) return null
7678

7779
const average = sum(arrayNumbers) / arrayNumbers.length
7880
return average
@@ -101,7 +103,8 @@ const anotherMixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 1
101103

102104
function avg(itemsArray) {
103105
if (itemsArray.length === 0) return null
104-
return sum(itemsArray) / itemsArray.length
106+
const result = Math.floor((sum(itemsArray) / itemsArray.length) * 100) / 100
107+
return result
105108
}
106109

107110
avg(anotherMixedArr)
@@ -126,9 +129,12 @@ function uniquifyArray(arr) {
126129
if (arr.length === 0) return null
127130

128131
for (i = 0; i < arr.length; i++) {
129-
for (j = ++i; j < arr.length; j++) {
132+
console.log(`i = ${i}`)
133+
for (j = i + 1; j < arr.length; j++) {
134+
console.log(`j = ${j}`)
130135
if (arr[i] === arr[j]) {
131136
arr.splice(j, 1);
137+
console.log(arr)
132138
}
133139
}
134140
}
@@ -169,7 +175,7 @@ const wordsCount = [
169175
];
170176

171177
function howManyTimes(arr, word) {
172-
if (arr.length === 0) return null
178+
if (arr.length === 0) return 0
173179
let wordTimes = 0
174180
for (i = 0; i < arr.length; i++) {
175181
if (arr[i] === word) {

0 commit comments

Comments
 (0)