Skip to content

Commit da6a914

Browse files
committed
exercise 21 into the right folder
1 parent dae7a47 commit da6a914

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Otra función sorprendente para los arreglos es el filtro. Repite toda el arreglo original y solo devuelve los valores que coinciden con una condición particular.
2+
3+
[Aquí está la documentación de la función de filtro en w3school](https://www.w3schools.com/jsref/jsref_filter.asp)
4+
5+
Por ejemplo, este algoritmo filtra el arreglo allNumbers y devuelve un nuevo arreglo con solo los números impares:
6+
7+
```js
8+
var allNumbers = [23,12,35,5,3,2,3,54,3,21,534,23,42,1];
9+
10+
var onlyOdds = allNumbers.filter(function(number){
11+
return (number % 2 > 0)
12+
});
13+
14+
console.log(onlyOdds);
15+
```
16+
17+
Instrucciones
18+
Completa el código para que llene el arreglo resultante de Nombres con solo los nombres que comienzan con la letra R
19+
Usa la función Array.filter
20+
21+
Pista:
22+
Aquí hay un video de 2:29min explicando array.filter
23+
https://www.youtube.com/watch?v=0qsFDFC2oEE
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# `21` Filter an Array
2+
3+
Another amazing function for arrays is `filter`.
4+
It loops the entire original array and only returns the values that match a particular condition.
5+
6+
[Here is the documentation of the filter function in w3school](https://www.w3schools.com/jsref/jsref_filter.asp)
7+
8+
For example, this algorithm filters the `allNumbers` array and returns a new array with only the odds numbers:
9+
10+
```js
11+
var allNumbers = [23,12,35,5,3,2,3,54,3,21,534,23,42,1];
12+
13+
var onlyOdds = allNumbers.filter(function(number){
14+
return (number % 2 > 0)
15+
});
16+
17+
console.log(onlyOdds);
18+
```
19+
20+
# 📝Instructions
21+
- Complete the code to make it fill the `resultingNames` array with only the names that start with letter R
22+
- Use the `array.filter` function
23+
24+
## 💡Hint
25+
Here is a 2:29min video explaining array.filter
26+
https://www.youtube.com/watch?v=0qsFDFC2oEE

exercises/21-Filter-an-array/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let allNames = ["Romario","Boby","Roosevelt","Emiliy", "Michael", "Greta", "Patricia", "Danzalee"];
2+
3+
//your code here
4+
5+
console.log(resultingNames);

exercises/21-Filter-an-array/test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const rewire = require("rewire");
4+
5+
let _log = console.log;
6+
let _buffer = '';
7+
global.console.log = console.log = jest.fn((text) => _buffer += text + "\n");
8+
9+
const app_content = fs.readFileSync(path.resolve(__dirname, './app.js'), 'utf8');
10+
11+
test("You should create the variable resultingNames.", function(){
12+
const file = rewire("./app.js");
13+
const myVar = file.__get__('resultingNames');
14+
expect(myVar).toBeTruthy();
15+
});
16+
17+
test('You have to use the console.log function to print the correct output.', function () {
18+
const app = require('./app.js');
19+
expect(console.log.mock.calls.length > 0).toBe(true);
20+
});
21+
22+
test('You should use the console.log to call the variable resultingNames', function () {
23+
const file = rewire("./app.js");
24+
const myVar = file.__get__('resultingNames');
25+
expect(console.log).toHaveBeenCalledWith(myVar);
26+
});
27+
28+
test('The output in the console should match the one in the instructions!', function () {
29+
const _app = rewire('./app.js');
30+
31+
let _allNames = ["Romario","Boby","Roosevelt","Emiliy", "Michael", "Greta", "Patricia", "Danzalee"];
32+
let _test = _allNames.filter(item=>item[0] === "R")
33+
expect(_buffer).toMatch(_test.map(n => n).join(","));
34+
});
35+
36+
37+
38+

0 commit comments

Comments
 (0)