Skip to content

Commit d755b30

Browse files
committed
Prepare good and bad imperative style examples
1 parent 9f468da commit d755b30

File tree

3 files changed

+112
-63
lines changed

3 files changed

+112
-63
lines changed

JavaScript/1-imperative-bad.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
// 1. Control flow
4+
// 2. Step by step execution
5+
// 3. Assignment statements
6+
// 4. Loops and conditions: for/if
7+
// 5. State variables and side effects
8+
// 6. Mutable data structures
9+
// 7. Model of the process
10+
11+
const fs = require('fs');
12+
13+
const data = fs.readFileSync('./cities.dat');
14+
if (data) {
15+
const lines = data.toString().split('\n');
16+
lines.pop();
17+
const table = [];
18+
let first = true;
19+
let max = 0;
20+
for (const line of lines) {
21+
if (first) {
22+
first = false;
23+
} else {
24+
const cells = line.split(',');
25+
const d = parseInt(cells[3]);
26+
if (d > max) max = d;
27+
table.push([cells[0], cells[1], cells[2], cells[3], cells[4]]);
28+
}
29+
}
30+
for (const row of table) {
31+
const a = Math.round(row[3] * 100 / max);
32+
row.push(a.toString());
33+
}
34+
table.sort((r1, r2) => r2[5] - r1[5]);
35+
for (const row of table) {
36+
let s = row[0].padEnd(18);
37+
s += row[1].padStart(10);
38+
s += row[2].padStart(8);
39+
s += row[3].padStart(8);
40+
s += row[4].padStart(18);
41+
s += row[5].padStart(6);
42+
console.log(s);
43+
}
44+
}

JavaScript/1-imperative-good.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict';
2+
3+
// 1. Control flow
4+
// 2. Step by step execution
5+
// 3. Assignment statements
6+
// 4. Loops and conditions: for/if
7+
// 5. State variables and side effects
8+
// 6. Mutable data structures
9+
// 7. Model of the process
10+
11+
const fs = require('fs');
12+
13+
function loadFile(fileName) {
14+
let data = null;
15+
try {
16+
data = fs.readFileSync(fileName);
17+
} catch (error) {
18+
console.log('Can\'t read file: ' + fileName);
19+
}
20+
return data;
21+
}
22+
23+
function parseFile(data) {
24+
const lines = data.toString().split('\n');
25+
lines.shift();
26+
const cities = [];
27+
for (const line of lines) {
28+
if (line) {
29+
const cells = line.split(',');
30+
const name = cells[0];
31+
const population = parseInt(cells[1]);
32+
const area = parseInt(cells[2]);
33+
const density = parseInt(cells[3]);
34+
const country = cells[4];
35+
cities.push({ name, population, area, density, country });
36+
}
37+
}
38+
return cities;
39+
}
40+
41+
function calculateDensityColumn(cities) {
42+
cities.sort((city1, city2) => city2.density - city1.density);
43+
const maxDensity = cities[0].density;
44+
for (const city of cities) {
45+
city.relative = Math.round(city.density * 100 / maxDensity);
46+
}
47+
}
48+
49+
function showTable(cities) {
50+
for (const city of cities) {
51+
const line = (
52+
city.name.padEnd(18) +
53+
city.population.toString().padStart(10) +
54+
city.area.toString().padStart(8) +
55+
city.density.toString().padStart(8) +
56+
city.country.padStart(18) +
57+
city.relative.toString().padStart(6)
58+
);
59+
console.log(line);
60+
}
61+
}
62+
63+
const data = loadFile('./cities.dat');
64+
if (data) {
65+
const cities = parseFile(data);
66+
calculateDensityColumn(cities);
67+
showTable(cities);
68+
}

JavaScript/1-imperative.js

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)