Skip to content

Commit 248ec00

Browse files
committed
NodeJS Basic tutorial
0 parents  commit 248ec00

28 files changed

+4960
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

0-console/0-console.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const displayMessage = (msg) => {
2+
console.log(msg);
3+
}
4+
5+
module.exports = displayMessage;

0-console/0-main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const displayMessage = require('./0-console');
2+
3+
displayMessage("Hello NodeJS!");

1-stdin/1-stdin.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
process.stdout.write("Welcome to Holberton School, what is your name?\n");
2+
3+
if (process.stdin.isTTY){
4+
process.stdin.on('data', (data) =>{
5+
process.stdout.write(`Your name is: ${data.toString()}`);
6+
process.exit();
7+
});
8+
} else {
9+
process.stdin.on('data', (data) =>{
10+
process.stdout.write(`Your name is: ${data.toString()}`);
11+
process.exit();
12+
});
13+
process.on('exit', () =>{
14+
process.stdout.write('This important software is now closing\n');
15+
});
16+
};

2-read_file/2-main_0.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const countStudents = require('./2-read_file');
2+
3+
countStudents("nope.csv");

2-read_file/2-main_1.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const countStudents = require('./2-read_file');
2+
3+
countStudents("database.csv");

2-read_file/2-read_file.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const fs = require('fs');
2+
3+
function countStudents(fileName){
4+
const students = {};
5+
const fields = {};
6+
let length = 0; //var, let, const
7+
try{
8+
const content = fs.readFileSync(fileName, 'utf-8');
9+
const lines = content.toString().split('\n');
10+
for (let i = 0; i < lines.length; i += 1){
11+
if (lines[i]){
12+
length += 1;
13+
const field = lines[i].toString().split(',');
14+
if (Object.prototype.hasOwnProperty.call(students, field[3])){
15+
students[field[3]].push(field[0]);
16+
}else {
17+
students[field[3]] = [field[0]];
18+
}
19+
if(Object.prototype.hasOwnProperty.call(fields, field[3])){
20+
fields[field[3]] += 1;
21+
} else {
22+
fields[field[3]] = 1;
23+
}
24+
}
25+
}
26+
const l = length - 1;
27+
console.log(`Number of students: ${l}`);
28+
for (const [key, value] of Object.entries(fields) ){
29+
if (key !== 'field'){
30+
console.log(`Number of students in ${key}: ${value}. List: ${students[key].join(', ')}`);
31+
}
32+
}
33+
34+
} catch (error){
35+
throw new Error('Cannot load the database');
36+
}
37+
}
38+
39+
module.exports = countStudents;

2-read_file/database.csv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
firstname,lastname,age,field
2+
Johann,Kerbrou,30,CS
3+
Guillaume,Salou,30,SWE
4+
Arielle,Salou,20,CS
5+
Jonathan,Benou,30,CS
6+
Emmanuel,Turlou,40,CS
7+
Guillaume,Plessous,35,CS
8+
Joseph,Crisou,34,SWE
9+
Paul,Schneider,60,SWE
10+
Tommy,Schoul,32,SWE
11+
Katie,Shirou,21,CS

3-read_file_async/3-main_0.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const countStudents = require('./3-read_file_async');
2+
countStudents("nope.csv")
3+
.then(() => {
4+
console.log("Done!");
5+
})
6+
.catch((error) => {
7+
console.log(error);
8+
});

3-read_file_async/3-main_1.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const countStudents = require('./3-read_file_async');
2+
countStudents("database.csv")
3+
.then(() => {
4+
console.log("Done!");
5+
})
6+
.catch((error) => {
7+
console.log(error);
8+
});
9+
console.log("After!");

0 commit comments

Comments
 (0)