forked from HackYourFuture/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathex7-mindPrivacy.js
More file actions
71 lines (65 loc) · 2.14 KB
/
ex7-mindPrivacy.js
File metadata and controls
71 lines (65 loc) · 2.14 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*------------------------------------------------------------------------------
Full description at: https://github.com/HackYourFuture/Assignments/tree/main/1-JavaScript/Week3#exercise-7-mind-the-privacy
1. Complete the `filterPrivateData()` function. It should take a single
parameter: the array of employee records.
2. It should create a _new_ array, containing employee data without the private
data.
3. Use object destructuring to extract the non-private properties from an
employee record (an `object`) and object literal shorthand to create a new
employee record with just the non-private parts (name, occupation and email).
4. Return the new array as the return value of the function.
5. Run the exercise and verify that it passes all the unit tests.
------------------------------------------------------------------------------*/
const employeeRecords = [
{
name: 'John',
occupation: 'developer',
gender: 'M',
email: 'john.doe@somewhere.net',
salary: 50000,
},
{
name: 'Jane',
occupation: 'manager',
gender: 'F',
email: 'jane.eyre@somewhere.net',
salary: 60000,
},
];
// ! Function under test
function filterPrivateData(records) {
// Create a new array by mapping over the original records
return records.map((record) => {
// Destructure the non-private properties
const { name, occupation, email } = record;
// Return a new object with only the allowed properties
return { name, occupation, email };
});
}
// ! Test functions (plain vanilla JavaScript)
function test1() {
console.log('Test 1: filterPrivateData should take one parameter');
console.assert(filterPrivateData.length === 1);
}
function test2() {
console.log('Test 2: gender and salary should be filtered out');
const expected = [
{
name: 'John',
occupation: 'developer',
email: 'john.doe@somewhere.net',
},
{
name: 'Jane',
occupation: 'manager',
email: 'jane.eyre@somewhere.net',
},
];
const result = filterPrivateData(employeeRecords);
console.assert(JSON.stringify(result) === JSON.stringify(expected));
}
function test() {
test1();
test2();
}
test();