Skip to content

Commit d20b5b6

Browse files
committed
Add for..in task
1 parent 9c7edda commit d20b5b6

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

Exercises/7-ages.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
const ages = persons => {
4+
// Use for..in to calculate age for each person
5+
// For example ages({
6+
// lenin: { born: 1870, died: 1924 },
7+
// mao: { born: 1893, died: 1976 },
8+
// gandhi: { born: 1869, died: 1948 },
9+
// hirohito: { born: 1901, died: 1989 },
10+
// })
11+
// should return {
12+
// lenin: 54,
13+
// mao: 83,
14+
// gandhi: 79,
15+
// hirohito: 88,
16+
// }
17+
};
18+
19+
module.exports = { ages };

Exercises/7-ages.test

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
({
2+
name: 'ages',
3+
length: [150, 190],
4+
cases: [
5+
[
6+
{
7+
lenin: { born: 1870, died: 1924 },
8+
mao: { born: 1893, died: 1976 },
9+
gandhi: { born: 1869, died: 1948 },
10+
hirohito: { born: 1901, died: 1989 },
11+
}, {
12+
lenin: 54,
13+
mao: 83,
14+
gandhi: 79,
15+
hirohito: 88,
16+
}
17+
]
18+
],
19+
test: ages => {
20+
const src = ages.toString();
21+
if (!src.includes('for (')) throw new Error('Use for..in loop');
22+
if (!src.includes(' in ')) throw new Error('Use for..in loop');
23+
}
24+
})

Solutions/7-ages.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const ages = persons => {
4+
const data = {};
5+
for (const name in persons) {
6+
const person = persons[name];
7+
data[name] = person.died - person.born;
8+
}
9+
return data;
10+
};
11+
12+
module.exports = { ages };

0 commit comments

Comments
 (0)