diff --git a/app/data/zadanie01/output.txt b/app/data/zadanie01/output.txt new file mode 100644 index 0000000..b984847 --- /dev/null +++ b/app/data/zadanie01/output.txt @@ -0,0 +1,2 @@ +1,2,7,20,56,22 +Suma elementów: 108 \ No newline at end of file diff --git a/app/data/zadanieDnia/output.txt b/app/data/zadanieDnia/output.txt new file mode 100644 index 0000000..1507e19 --- /dev/null +++ b/app/data/zadanieDnia/output.txt @@ -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); + } +}); \ No newline at end of file diff --git a/app/zadanie01.js b/app/zadanie01.js index 8c20173..311e492 100644 --- a/app/zadanie01.js +++ b/app/zadanie01.js @@ -1 +1,16 @@ -//Twój kod \ No newline at end of file +//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); + } +}); \ No newline at end of file diff --git a/app/zadanie02.js b/app/zadanie02.js index 8c20173..1cd0052 100644 --- a/app/zadanie02.js +++ b/app/zadanie02.js @@ -1 +1,24 @@ -//Twój kod \ No newline at end of file +//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); + } +}); \ No newline at end of file diff --git a/app/zadanieDnia.js b/app/zadanieDnia.js index 8c20173..d9eea72 100644 --- a/app/zadanieDnia.js +++ b/app/zadanieDnia.js @@ -1 +1,26 @@ -//Twój kod \ No newline at end of file +//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) => { + 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); + } +}); \ No newline at end of file diff --git a/test.js b/test.js new file mode 100644 index 0000000..21b2d82 --- /dev/null +++ b/test.js @@ -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); + } +}) \ No newline at end of file diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..6769dd6 --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +Hello world! \ No newline at end of file