Skip to content

Commit f10cdbc

Browse files
committed
Add 2-read_file.js file
1 parent 7592c8c commit f10cdbc

File tree

4 files changed

+8324
-3
lines changed

4 files changed

+8324
-3
lines changed

0x05-Node_JS_basic/1-stdin.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ process.stdin.setEncoding('utf8');
22

33
console.log('Welcome to Holberton School, what is your name?');
44

5-
process.stdin.on('data', (input) => {
6-
const name = input.trim();
5+
process.stdin.on('readable', () => {
6+
const name = process.stdin.read();
77

88
if (name.length > 0) {
9-
console.log(`Your name is: ${input}`);
9+
process.stdout.write(`Your name is: ${name}`);
1010
process.exit();
1111
}
1212
});

0x05-Node_JS_basic/2-read_file.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const fs = require('fs');
2+
3+
/**
4+
* Counts the students in a CSV data file.
5+
* @param {String} path The path to the CSV data file.
6+
* @returns {void}
7+
*/
8+
9+
function countStudents(path) {
10+
try {
11+
const data = fs.readFileSync(path, 'utf8').split('\n');
12+
13+
if (data.length === 0) {
14+
throw new Error('Cannot load the database');
15+
}
16+
const students = data.map((line) => line.split(','));
17+
18+
console.log(`Number of students: ${students.length - 1}`);
19+
20+
const fields = students[0];
21+
const fieldIndex = fields.indexOf('field');
22+
23+
if (fieldIndex === -1) {
24+
throw new Error('Field column not found');
25+
}
26+
27+
const validStudents = students
28+
.slice(1)
29+
.filter((student) => student[fieldIndex] !== undefined && student[fieldIndex] !== '');
30+
31+
const classes = validStudents.reduce((acc, student) => {
32+
const className = student[fieldIndex];
33+
if (!acc[className]) {
34+
acc[className] = [];
35+
}
36+
acc[className].push(student[fields.indexOf('firstname')]);
37+
return acc;
38+
}, {});
39+
40+
for (const [className, studentList] of Object.entries(classes)) {
41+
console.log(`Number of students in ${className}: ${studentList.length}. List: ${studentList.join(', ')}`);
42+
}
43+
} catch (error) {
44+
console.error(error.message);
45+
}
46+
}
47+
48+
countStudents('database.csv');
49+
module.exports = countStudents;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const fs = require('fs');
2+
3+
/**
4+
* Counts the students in a CSV data file.
5+
* @param {String} dataPath The path to the CSV data file.
6+
* @author Bezaleel Olakunori <https://github.com/B3zaleel>
7+
*/
8+
const countStudents = (dataPath) => new Promise((resolve, reject) => {
9+
fs.readFile(dataPath, 'utf-8', (err, data) => {
10+
if (err) {
11+
reject(new Error('Cannot load the database'));
12+
}
13+
if (data) {
14+
const fileLines = data
15+
.toString('utf-8')
16+
.trim()
17+
.split('\n');
18+
const studentGroups = {};
19+
const dbFieldNames = fileLines[0].split(',');
20+
const studentPropNames = dbFieldNames
21+
.slice(0, dbFieldNames.length - 1);
22+
23+
for (const line of fileLines.slice(1)) {
24+
const studentRecord = line.split(',');
25+
const studentPropValues = studentRecord
26+
.slice(0, studentRecord.length - 1);
27+
const field = studentRecord[studentRecord.length - 1];
28+
if (!Object.keys(studentGroups).includes(field)) {
29+
studentGroups[field] = [];
30+
}
31+
const studentEntries = studentPropNames
32+
.map((propName, idx) => [propName, studentPropValues[idx]]);
33+
studentGroups[field].push(Object.fromEntries(studentEntries));
34+
}
35+
36+
const totalStudents = Object
37+
.values(studentGroups)
38+
.reduce((pre, cur) => (pre || []).length + cur.length);
39+
console.log(`Number of students: ${totalStudents}`);
40+
for (const [field, group] of Object.entries(studentGroups)) {
41+
const studentNames = group.map((student) => student.firstname).join(', ');
42+
console.log(`Number of students in ${field}: ${group.length}. List: ${studentNames}`);
43+
}
44+
resolve(true);
45+
}
46+
});
47+
});
48+
49+
module.exports = countStudents;

0 commit comments

Comments
 (0)