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

rozwiązania #6

Open
wants to merge 1 commit into
base: master
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
2 changes: 2 additions & 0 deletions app/data/zadanie01/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1,2,7,20,56,22
Suma elementów: 108
24 changes: 24 additions & 0 deletions app/data/zadanieDnia/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//tWóJ KoD
CoNsT Fs = rEqUiRe('Fs');

cOnSt pAtH = './dAtA/ZaDaNiE02/';

CoNsT ReAdCoNtEnT = (fIlEpAtH) => {
Fs.rEaDfIlE(FiLePaTh, 'uTf8', (eRr, DaTa) => {
iF (!ErR) {
cOnSoLe.lOg(fIlEpAtH.InClUdEs('.jSoN') ? JsOn.pArSe(dAtA) : dAtA);
} ElSe {
cOnSoLe.lOg(`BłĄd pOdCzAs cZyTaNiA PlIkU:\n`, eRr);
}
});
}

Fs.rEaDdIr(pAtH, (ErR, fIlEs) => {
iF (!ErR) {
fIlEs.fOrEaCh(fIlE => {
rEaDcOnTeNt(pAtH + FiLe);
});
} eLsE {
CoNsOlE.LoG('bŁąD PoDcZaS CzYtAnIa lIsTy pLiKóW.\n', eRr);
}
});
17 changes: 16 additions & 1 deletion app/zadanie01.js
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
//Twój kod
//Twój kod
const fs = require('fs');
const inputFile = './data/zadanie01/input.json';
const outputFile = './data/zadanie01/output.txt';

fs.readFile(inputFile, 'utf8', (err, data) => {
if(!err) {
const values = JSON.parse(data);
const sum = values.reduce((prev, next) => prev + next);
fs.writeFile(outputFile, `${values}\nSuma elementów: ${sum}`, err => {
console.log(!err ? 'Policzone!' : 'Błąd podczas zapisu:\n' + err);
})
} else {
console.log('Błąd podczas odczytu:\n' + err);
}
});
25 changes: 24 additions & 1 deletion app/zadanie02.js
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
//Twój kod
//Twój kod
const fs = require('fs');

const path = './data/zadanie02/';

const readContent = (filePath) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (!err) {
console.log(filePath.includes('.json') ? JSON.parse(data) : data);
} else {
console.log(`Błąd podczas czytania pliku:\n`, err);
}
});
}

fs.readdir(path, (err, files) => {
if (!err) {
files.forEach(file => {
readContent(path + file);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super, tak jest dużo czytalniej.

});
} else {
console.log('Błąd podczas czytania listy plików.\n', err);
}
});
27 changes: 26 additions & 1 deletion app/zadanieDnia.js
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
//Twój kod
//Twój kod
const fs = require('fs');

const defaultFilePath = './data/zadanieDnia/test.txt';
const filePath = process.argv[2];
const outputPath = './data/zadanieDnia/output.txt'

const setCase = (letter, index) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bardzo fajny podział na osobne funkcje!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No właśnie mam wrażenie ze setCase jest już niepotrzebny przy zoptymalizowaniu kodu do jednego wiersza.

Bardziej jestem niezadowolony z pętli for, która próbowałem zastąpić: text.split().forEach((letter, index) => ...itd, ale nie wiem czemu otrzymywałem pod letter cały tekst. Czy split(’’) zadziała?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tak, split(''), bo "If separator is omitted, the array returned contains one element consisting of the entire string. If separator is an empty string, str is converted to an array of characters." (https://developer.mozilla.org/pl/docs/Web/JavaScript/Referencje/Obiekty/String/split)

return index % 2 != 0 ? letter.toUpperCase() : letter.toLowerCase();
}

const transformText = (text) => {
let result = '';
for (let i = 0, len = text.length; i < len; i++) { //czy jest zgrabniejsza metoda iterowania po tekście pozwalająca przekazać indeks litery?
result += setCase(text[i], i);
}
return result;
}

fs.readFile(filePath || defaultFilePath, 'utf8', (err, data) => {
if (!err) {
fs.writeFile(outputPath, transformText(data), err => console.log(!err ? 'Posiano trawę tutaj: ' + outputPath : 'Błąd podczas zapisywania wyniku do pliku:\n' + err));
} else {
console.log('Błąd podczas czytania pliku:\n', err);
}
});
26 changes: 26 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const fs = require('fs');
const fileName = 'test.txt'

// fs.writeFile(fileName, 'Hello world!', err => {
// if (!err) {
// console.log('Zapisano.');

// fs.readFile(fileName, 'utf8', (err, data) => {
// console.log(!err
// ? 'Poprawnie odczytano plik.\n' + data
// : 'Błąd podczas odczytywania pliku.\n' + err
// );
// });
// } else {
// console.log('Błąd podczas zapisywania pliku:\n' + err)
// }

// });

fs.readdir('./ap', (err, files) => {
if(!err) {
console.log(files);
} else {
console.log(err);
}
})
1 change: 1 addition & 0 deletions test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world!