Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(m4): add solution exe-001 display-the-head-of-a-file #3543

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions projects/m4/001-display-the-head-of-a-file/js/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Line 1: This is the first line.
Line 2: This is the second line.
Line 3: This is the third line.
Line 4: This is the fourth line.
Line 5: This is the fifth line.
Line 6: This is the sixth line.
Line 7: This is the seventh line.
Line 8: This is the eighth line.
Line 9: This is the ninth line.
Line 10: This is the tenth line.
Line 11: This is the eleventh line.
Line 12: This is the twelfth line.
Line 13: This is the thirteenth line.
Line 14: This is the fourteenth line.
Line 15: This is the fifteenth line.
Line 16: This is the sixteenth line.
Line 17: This is the seventeenth line.
Line 18: This is the eighteenth line.
Line 19: This is the nineteenth line.
Line 20: This is the twentieth line.
Line 21: This is the twenty-first line.
Line 22: This is the twenty-second line.
Line 23: This is the twenty-third line.
Line 24: This is the twenty-fourth line.
4 changes: 4 additions & 0 deletions projects/m4/001-display-the-head-of-a-file/js/example2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Line 15: This is the fifteenth line.
Line 16: This is the sixteenth line.
Line 17: This is the seventeenth line.
Line 18: This is the eighteenth line.
5 changes: 5 additions & 0 deletions projects/m4/001-display-the-head-of-a-file/js/example3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Ciao come stai?
Line 15: This is the fifteenth line.
Line 16: This is the sixteenth line.
Line 17: This is the seventeenth line.
Line 18: This is the eighteenth line.
36 changes: 36 additions & 0 deletions projects/m4/001-display-the-head-of-a-file/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { readFile } = require('fs/promises');
let filePath = './example.txt';
let filePath2 = './example2.txt';
let filePath3 = './example3.txt';
const numberLines = 5;

readFile(filePath)
.then((data) => {
console.log(data.toString().split('\r\n').slice(0, 10).join('\r\n')); // The first 10 lines
console.log(
data.toString().split('\r\n').slice(0, numberLines).join('\r\n') // The first 5 lines
);
console.log(
// The first 3 lines
`${filePath}\n ${data
.toString()
.split('\r\n')
.slice(0, numberLines - 2)
.join('\r\n')}`
);
})

.catch((error) => {
console.log(error);
});

// Multiple files
Promise.all([readFile(filePath), readFile(filePath2), readFile(filePath3)])
.then(([data1, data2, data3]) => {
console.log(data1.toString().split('\r\n').slice(0, 1).join('\r\n'));
console.log(data2.toString().split('\r\n').slice(0, 1).join('\r\n'));
console.log(data3.toString().split('\r\n').slice(0, 1).join('\r\n'));
})
.catch((error) => {
console.log(error);
});