forked from HackYourFuture/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathex1-doubleEvenNumbers.test.js
More file actions
35 lines (30 loc) · 1.26 KB
/
ex1-doubleEvenNumbers.test.js
File metadata and controls
35 lines (30 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*------------------------------------------------------------------------------
Full description at: https://github.com/HackYourFuture/Assignments/tree/main/1-JavaScript/Week4#exercise-1-the-odd-ones-out
The `doubleEvenNumbers` function returns only the even numbers in the array
passed as the `numbers` parameter and doubles them.
Let's rewrite it (or _refactor_ it, as experienced developers would call it):
- Using the `map` and `filter` functions, rewrite the function body of
`doubleEvenNumbers`.
------------------------------------------------------------------------------*/
// ! Function to be tested
function doubleEvenNumbers(numbers) {
// TODO rewrite the function body using `map` and `filter`.
const evenMumbers = numbers
.filter((num) => num % 2 === 0)
.map((num) => num * 2);
// const newNumbers = [];
// for (let i = 0; i < numbers.length; i++) {
// if (numbers[i] % 2 === 0) {
// newNumbers.push(numbers[i] * 2);
// }
// }
return evenMumbers;
}
// ! Unit test (using Jest)
describe('js-wk3-ex1-doubleEvenNumbers', () => {
test('doubleEvenNumbers should take the even numbers and double them', () => {
const actual = doubleEvenNumbers([1, 2, 3, 4]);
const expected = [4, 8];
expect(actual).toEqual(expected);
});
});