Skip to content

Commit cb69917

Browse files
committed
Finish bonus ironhack-labs#3
1 parent c12dea8 commit cb69917

File tree

2 files changed

+68
-41
lines changed

2 files changed

+68
-41
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"liveServer.settings.port": 5501
3+
}

src/functions-and-arrays.js

Lines changed: 65 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ function sumNumbers(arrOfNums) {
6666
return sum;
6767
}
6868

69+
sumNumbers(numbers);
70+
6971
//or with for of loop:
7072

7173
// function sumNumbers(arrOfNums) {
@@ -77,24 +79,49 @@ function sumNumbers(arrOfNums) {
7779
// return sum;
7880
// }
7981

82+
//Iteration #3 BONUS:
8083

81-
82-
83-
sumNumbers(numbers);
84+
function sum(arr) {
85+
if (arr.length === 0) {
86+
return 0;
87+
} else {
88+
let total = 0;
89+
for (let i = 0; i < arr.length; i++) {
90+
let element = arr[i];
91+
console.log(element);
92+
if (element === 0) {
93+
return 0;
94+
} else if (typeof element === "number") {
95+
total += element;
96+
} else if (typeof element === "string") {
97+
total += element.length;
98+
} else if (typeof element === "boolean") {
99+
total += Number(element);
100+
} else {
101+
throw new Error("Unsupported data type sir or ma'am");
102+
}
103+
}
104+
// for (let i = 0; i < arr.length; i++) {
105+
// total += arr[i];
106+
// console.log(total);
107+
// }
108+
return total;
109+
}
110+
}
84111

85112
// Iteration #4: Calculate the average
86113
// Level 1: Array of numbers
87114
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
88115

89116
function averageNumbers(arrOfNums) {
90117
if (arrOfNums.length === 0) return null;
91-
let sum = 0;
92-
let average;
93-
for (let i = 0; i < arrOfNums.length; i++) {
94-
sum += arrOfNums[i];
95-
average = sum / arrOfNums.length;
96-
}
97-
return average;
118+
let sum = 0;
119+
let average;
120+
for (let i = 0; i < arrOfNums.length; i++) {
121+
sum += arrOfNums[i];
122+
average = sum / arrOfNums.length;
123+
}
124+
return average;
98125
}
99126

100127
averageNumbers(numbersAvg);
@@ -115,16 +142,15 @@ const wordsArr = [
115142

116143
function averageWordLength(arrOfWords) {
117144
if (arrOfWords.length === 0) return null;
118-
145+
119146
let total = 0;
120-
for (let i = 0; i < arrOfWords.length; i++) {
121-
let word = arrOfWords[i];
122-
total += word.length;
123-
console.log("total", total);
124-
}
125-
let average = total / arrOfWords.length;
126-
return average;
127-
147+
for (let i = 0; i < arrOfWords.length; i++) {
148+
let word = arrOfWords[i];
149+
total += word.length;
150+
console.log("total", total);
151+
}
152+
let average = total / arrOfWords.length;
153+
return average;
128154
}
129155

130156
averageWordLength(wordsArr);
@@ -146,16 +172,16 @@ const wordsUnique = [
146172

147173
function uniquifyArray(array) {
148174
if (array.length === 0) return null;
149-
175+
150176
let newArr = [];
151-
for (let i = 0; i < array.length; i++) {
152-
let currentWord = array[i];
153-
console.log("currentWord", currentWord);
154-
if (newArr.indexOf(currentWord) < 0) {
155-
newArr.push(currentWord);
156-
}
177+
for (let i = 0; i < array.length; i++) {
178+
let currentWord = array[i];
179+
console.log("currentWord", currentWord);
180+
if (newArr.indexOf(currentWord) < 0) {
181+
newArr.push(currentWord);
157182
}
158-
return newArr;
183+
}
184+
return newArr;
159185
}
160186

161187
uniquifyArray(wordsUnique);
@@ -174,16 +200,15 @@ const wordsFind = [
174200

175201
function doesWordExist(arr, searchWord) {
176202
if (arr.length === 0) return null;
177-
203+
178204
let match = false;
179-
for (let i = 0; (i < arr.length) & !match; i++) {
180-
if (arr[i] === searchWord) {
181-
match = true;
182-
break;
183-
}
205+
for (let i = 0; (i < arr.length) & !match; i++) {
206+
if (arr[i] === searchWord) {
207+
match = true;
208+
break;
184209
}
185-
return match;
186-
210+
}
211+
return match;
187212
}
188213
doesWordExist(wordsFind, "truth");
189214

@@ -205,13 +230,12 @@ const wordsCount = [
205230
function howManyTimes(arr, searchWord) {
206231
let count = 0;
207232
if (arr.length === 0) return count;
208-
for (let i = 0; i < arr.length; i++) {
209-
if (arr[i] === searchWord) {
210-
count += 1;
211-
}
233+
for (let i = 0; i < arr.length; i++) {
234+
if (arr[i] === searchWord) {
235+
count += 1;
212236
}
213-
return count;
214-
237+
}
238+
return count;
215239
}
216240

217241
// Iteration #8: Bonus

0 commit comments

Comments
 (0)