Skip to content

Commit 5609c17

Browse files
Lab casi terminado, No entendi muy bien que se tiene que realizar en Iteration ironhack-labs#8: Bonus
1 parent 13ac3f3 commit 5609c17

File tree

1 file changed

+108
-16
lines changed

1 file changed

+108
-16
lines changed

src/functions-and-arrays.js

Lines changed: 108 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,111 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
2+
function maxOfTwoNumbers(Num1,Num2){
3+
if(Num1>Num2){
4+
return(Num1)
5+
}else{
6+
return(Num2)
7+
}
8+
}
9+
console.log(maxOfTwoNumbers(10,20))
310

11+
console.log(Math.max(10,20))
412

513

614
// Iteration #2: Find longest word
715
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
816

9-
function findLongestWord() {}
17+
function findLongestWord(palabras) {
18+
let max = palabras.length;
19+
palabras.map(palabra => max = Math.max(max, palabra.length));
20+
result = palabras.filter(palabra => palabra.length == max);
21+
return result;
22+
}
23+
24+
console.log(findLongestWord(words))
1025

1126

1227

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

16-
function sumNumbers() {}
17-
18-
31+
function sumNumbers(array) {
32+
const suma = array.reduce((total,number)=>{
33+
return total +=number
34+
},0)
35+
return suma
36+
}
37+
console.log(sumNumbers(numbers))
1938

2039
// Iteration #3.1 Bonus:
21-
function sum() {}
40+
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
41+
function sum() {
42+
const stringToNumber = mixedArr.map(dato=>{
43+
if( typeof dato === "string"){
44+
return (dato.length)
45+
}
46+
if( typeof dato === "boolean"){
47+
dato=1
48+
return (dato)
49+
}
50+
return dato
51+
})
52+
const Resultado = stringToNumber.reduce((total,number)=>{
53+
return total+= number
54+
},0)
55+
return Resultado
56+
}
57+
58+
console.log(sum(mixedArr))
2259

2360

2461

2562
// Iteration #4: Calculate the average
2663
// Level 1: Array of numbers
2764
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2865

29-
function averageNumbers() {}
66+
function averageNumbers(Numbers) {
67+
const Suma = Numbers.reduce((total,number)=>{
68+
return total+=number
69+
})
70+
return Suma/Numbers.length
71+
}
72+
73+
console.log(averageNumbers(numbersAvg))
3074

3175

3276
// Level 2: Array of strings
3377
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
3478

35-
function averageWordLength() { }
36-
79+
function averageWordLength(array) {
80+
const wordsArrLength = array.map(palabra=>{
81+
return palabra.length
82+
})
83+
const suma = wordsArrLength.reduce((total,number)=>{
84+
return total+=number
85+
})
86+
return suma/array.length
87+
}
88+
console.log(averageWordLength(wordsArr))
3789
// Bonus - Iteration #4.1
38-
function avg() {}
90+
91+
function avg(array) {
92+
const stringToNumber = array.map(dato=>{
93+
if( typeof dato === "string"){
94+
return (dato.length)
95+
}
96+
if( typeof dato === "boolean"){
97+
dato=1
98+
return (dato)
99+
}
100+
return dato
101+
})
102+
const Resultado = stringToNumber.reduce((total,number)=>{
103+
return total+= number
104+
},0)
105+
return Resultado / array.length
106+
}
107+
108+
console.log(sum(mixedArr))
39109

40110
// Iteration #5: Unique arrays
41111
const wordsUnique = [
@@ -52,15 +122,31 @@ const wordsUnique = [
52122
'bring'
53123
];
54124

55-
function uniquifyArray() {}
125+
function uniquifyArray(palabras) {
126+
127+
let result = palabras.filter((palabra,index)=>{
128+
return palabras.indexOf(palabra) === index;
129+
})
130+
return result;
131+
}
56132

133+
console.log(uniquifyArray(wordsUnique))
57134

58135

59136
// Iteration #6: Find elements
60137
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
138+
const palabraAbuscar = "trouble"
139+
140+
function doesWordExist(array) {
141+
const find = array.find(palabra=>palabraAbuscar === palabra)
142+
if(find){
143+
return "Si existe"
144+
}else{
145+
return "No existe"
146+
}
147+
}
61148

62-
function doesWordExist() {}
63-
149+
console.log(doesWordExist(wordsFind))
64150

65151

66152
// Iteration #7: Count repetition
@@ -77,9 +163,15 @@ const wordsCount = [
77163
'disobedience',
78164
'matter'
79165
];
166+
const palabra = "matter"
167+
function howManyTimes(array) {
168+
const palabraRepetida = array.filter(palabraDeArray=>{
169+
return palabraDeArray.includes(palabra)
170+
})
171+
return palabraRepetida
80172

81-
function howManyTimes() {}
82-
173+
}
174+
console.log(howManyTimes(wordsCount).length)
83175

84176

85177
// Iteration #8: Bonus
@@ -108,7 +200,7 @@ const matrix = [
108200

109201
function greatestProduct() {}
110202

111-
203+
//No entendí Iteration 8
112204

113205

114206
// The following is required to make unit tests work.

0 commit comments

Comments
 (0)