-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path6-completed_tasks.js
More file actions
executable file
·63 lines (57 loc) · 1.3 KB
/
6-completed_tasks.js
File metadata and controls
executable file
·63 lines (57 loc) · 1.3 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
#!/usr/bin/node
const URL = process.argv[2];
const request = require('request');
request.get(URL, { json: true }, (error, response, body) => {
if (error) {
console.log(error);
return;
}
if (response.statusCode !== 200) {
console.log(error);
return;
}
const todos = {};
body.forEach((todo) => {
if (todo.completed) {
if (!todos[todo.userId]) {
todos[todo.userId] = 1;
} else {
todos[todo.userId]++;
}
}
});
const length = Object.keys(todos).length;
if (length === 0) {
console.log('{}');
return;
}
if (length === 2) {
let output = '{ ';
let isFirst = true;
for (const key in todos) {
if (!isFirst) {
output += ', ';
}
output += `'${key}': ${todos[key]}`;
isFirst = false;
}
output += ' }';
console.log(output);
} else {
let task = 0;
for (const key in todos) {
if (task === 0) {
if (length !== 1) {
console.log('{ \'' + key + '\': ' + todos[key] + ',');
} else {
console.log('{ \'' + key + '\': ' + todos[key] + ' }');
}
} else if (task === length - 1) {
console.log(' \'' + key + '\': ' + todos[key] + ' }');
} else {
console.log(` '${key}': ${todos[key]},`);
}
task++;
}
}
});