Skip to content

Commit dfe8c46

Browse files
committed
Iteration ironhack-labs#6 and ironhack-labs#7 Complete
1 parent 8d550de commit dfe8c46

File tree

1 file changed

+63
-13
lines changed

1 file changed

+63
-13
lines changed

src/functions-and-arrays.js

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,30 @@ console.log(sumNumbers(numbers));
6464

6565

6666
// Iteration #3.1 Bonus:
67-
function sum(array) {
68-
let sumArray = 0;
67+
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
6968

70-
for (let i = 0; i < array.length; i++) {
71-
sumArray += array[i];
72-
}
73-
return sumArray;
69+
function sum(array) {
70+
let sumArray = 0;
71+
let sumLetter = 0;
72+
73+
for (let i = 0; i < array.length; i++) {
74+
if (typeof array[i] === 'string' ) {
75+
sumLetter += array[i].length;
76+
} else if (typeof array[i] === true) {
77+
sumLetter += 1;
78+
} else if (typeof array[i] === false) {
79+
sumLetter += 0;
80+
} else {
81+
array[i];
82+
}
83+
84+
sumArray = array[i] + sumLetter;
85+
86+
}
87+
return sumArray;
7488
}
7589

76-
console.log(sum([5, 6, 5]));
90+
console.log(sum(mixedArr));
7791

7892
//initialize variable for our sum number to store.
7993
// Using for loop, iterate through an array; as we do not know how many values inside an array, we need to use array.length;
@@ -88,13 +102,27 @@ console.log(sum([5, 6, 5]));
88102
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
89103

90104
function averageNumbers(array) {
91-
let sumArray = 0;
105+
if (array.length === 0) {
106+
return null
107+
}
108+
return sumNumbers(array) / array.length;
109+
110+
//Longer solution
111+
92112

93-
for (let i = 0; i < array.length; i++) {
94-
sumArray += array[i];
113+
function averageNumbers(array) {
114+
if (array.length === 0) {
115+
return null
116+
}
117+
118+
let sumArray = 0;
119+
120+
for (let i = 0; i < array.length; i++) {
121+
sumArray += array[i];
95122
}
96123
return sumArray / array.length;
97124
}
125+
}
98126

99127
console.log(averageNumbers(numbersAvg));
100128

@@ -245,9 +273,20 @@ function uniquifyArray(array) {
245273

246274

247275
// Iteration #6: Find elements
248-
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
276+
const words = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
249277

250-
function doesWordExist() {}
278+
function doesWordExist(array, wordToSearch) {
279+
280+
for (let i = 0; i < array.length; i++) {
281+
if (array.includes(wordToSearch)) {
282+
return true;
283+
} else {
284+
return false;
285+
}
286+
}
287+
}
288+
289+
console.log(doesWordExist(words, 'starting'));
251290

252291

253292

@@ -266,7 +305,18 @@ const wordsCount = [
266305
'matter'
267306
];
268307

269-
function howManyTimes() {}
308+
function howManyTimes(array, wordToSearch) {
309+
let count = 0;
310+
311+
for (let i = 0; i < array.length; i++) {
312+
if (array[i] === wordToSearch) {
313+
count++;
314+
}
315+
}
316+
return count;
317+
}
318+
319+
console.log(howManyTimes(wordsCount,'matter'));
270320

271321

272322

0 commit comments

Comments
 (0)