-
Notifications
You must be signed in to change notification settings - Fork 252
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
base: master
Are you sure you want to change the base?
rozwiązania #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
1,2,7,20,56,22 | ||
Suma elementów: 108 |
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); | ||
} | ||
}); |
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); | ||
} | ||
}); |
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); | ||
}); | ||
} else { | ||
console.log('Błąd podczas czytania listy plików.\n', err); | ||
} | ||
}); |
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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bardzo fajny podział na osobne funkcje! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tak, |
||
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); | ||
} | ||
}); |
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); | ||
} | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello world! |
There was a problem hiding this comment.
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.