-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.js
More file actions
135 lines (114 loc) · 2.71 KB
/
demo.js
File metadata and controls
135 lines (114 loc) · 2.71 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict';
function greet(name) {
if (!name) {
return 'Hello, Stranger';
}
return 'Hello, ' + name;
}
const toUpper = (value = '') => value.toUpperCase();
const safeDivide = (a, b = 1) => {
if (b === 0) {
throw new Error('Cannot divide by zero');
}
return a / b;
};
const logger = (...args) => console.log.apply(console, args);
class Reporter {
static headers() {
return ['name', 'score'];
}
constructor(data) {
this.data = data;
}
summary() {
return this.data.map((item) => `${item.name}: ${item.score}`);
}
}
function buildData(seed) {
let total = 0;
const records = [];
for (let i = 0; i < seed.length; i += 1) {
const entry = seed[i];
total += entry.score;
records.push({ name: entry.name, score: entry.score });
}
let average = 0;
if (seed.length > 0) {
average = total / seed.length;
}
const stats = {
total,
average,
};
return { records, stats };
}
function classifyScore(score) {
switch (true) {
case score >= 90:
return 'A';
case score >= 80:
return 'B';
case score >= 70:
return 'C';
default:
return 'D';
}
}
function summarize(seed) {
const prepared = buildData(seed);
const report = new Reporter(prepared.records);
const grades = [];
for (const record of prepared.records) {
grades.push({ name: record.name, grade: classifyScore(record.score) });
}
const indices = [];
for (const key in prepared.stats) {
if (Object.prototype.hasOwnProperty.call(prepared.stats, key)) {
indices.push(`${key}:${prepared.stats[key]}`);
}
}
let attempts = 0;
while (attempts < 3) {
logger('Attempt', attempts + 1, 'summary ready');
attempts += 1;
}
try {
const lines = report.summary();
const headline = Reporter.headers().join(', ');
return {
headline,
lines,
indices,
grades,
upperNames: seed.map((entry) => toUpper(entry.name)),
safeDiv: safeDivide(prepared.stats.total, seed.length || 1),
};
} catch (err) {
logger('Failed to summarize:', err.message);
throw err;
} finally {
logger('Summary finished');
}
}
const sample = [
{ name: 'Alice', score: 95 },
{ name: 'Bob', score: 82 },
{ name: 'Cara', score: 76 },
];
if (require.main === module) {
const result = summarize(sample);
logger('Result headline:', result.headline);
logger('Lines:', result.lines.join('; '));
logger('Indices:', result.indices.join('; '));
logger('Grades:', result.grades.map((g) => `${g.name}:${g.grade}`).join('; '));
logger('Upper:', result.upperNames.join(', '));
logger('Safe divide:', result.safeDiv);
}
module.exports = {
greet,
toUpper,
safeDivide,
buildData,
classifyScore,
summarize,
};