diff --git a/lesson3/challenges/convertBlocking.js b/lesson3/challenges/convertBlocking.js index d5364cc..deef690 100644 --- a/lesson3/challenges/convertBlocking.js +++ b/lesson3/challenges/convertBlocking.js @@ -3,5 +3,9 @@ var fs = require('fs'); -var contents = fs.readFileSync('hello.html'); -console.log(contents.toString()); \ No newline at end of file +// var contents = fs.readFileSync('hello.html'); +// console.log(contents.toString()); + +fs.readFile('hello.html','utf-8', (err, data) => { + console.log(data); +}); diff --git a/lesson3/challenges/copyFile.js b/lesson3/challenges/copyFile.js index 72504a7..2c9e63b 100644 --- a/lesson3/challenges/copyFile.js +++ b/lesson3/challenges/copyFile.js @@ -1,6 +1,21 @@ // Copy contents from hello.html to hello-copy.html var fs = require('fs') + // 1. Do this synchronously first +// const encoding = 'utf8'; +// const contents = fs.readFileSync('hello.html', encoding); +// fs.writeFile('hello-copy.html', contents, encoding, (err) => { +// console.log(err); +// }) -// 2. Do the same thing asynchronously \ No newline at end of file +// 2. Do the same thing asynchronously +fs.readFile('hello.html', (err, data) => { + if (err) { + console.log(err); + } else { + fs.writeFile('hello-copy.html', data, (err) => { + console.log(err); + }) + } +}) diff --git a/lesson3/challenges/rectangle.js b/lesson3/challenges/rectangle.js index 7486b12..2365eb1 100644 --- a/lesson3/challenges/rectangle.js +++ b/lesson3/challenges/rectangle.js @@ -33,9 +33,16 @@ function rectangle(length, width, callback) { function sloveRectangle(err, result) { // To be implemented + if (err) { + console.log(err.message); + } else { + // console.log(`- length: ${length}, width: ${width}`) + console.log(`- The area : ${result.area()}`); + console.log(`- The perimeter : ${result.perimeter()}`); + } } // Test rectangle(2, 4, sloveRectangle); rectangle(3, 5, sloveRectangle); -rectangle(-3, 5, sloveRectangle); \ No newline at end of file +rectangle(-3, 5, sloveRectangle); diff --git a/lesson3/challenges/tasks.js b/lesson3/challenges/tasks.js new file mode 100644 index 0000000..1512f23 --- /dev/null +++ b/lesson3/challenges/tasks.js @@ -0,0 +1,75 @@ +/* +$ node tasks.js list +$ node tasks.js clear +$ node tasks.js add +*/ + +const fs = require('fs'); + +const targetFile = 'tasks.txt'; +const defaultContents = '[]'; +let tasks; + +try { + tasks = parseFile(targetFile); +} catch (e) { + fs.writeFileSync(targetFile, defaultContents, 'utf8', (err) => { + console.log(err); + }) + tasks = parseFile(targetFile); +} + +let command = process.argv[2]; + +switch (command) { + case 'list': + listTasks(); + break; + case 'clear': + clearTasks(); + break; + case 'add': + addTask(); + break; + default: + help(); +} + +function parseFile(fileName){ + return JSON.parse(fs.readFileSync(fileName, 'utf-8')); +} + +function listTasks() { + if (tasks.length === 0) { + console.log('Found no tasks.'); + } else { + console.log('Found', tasks.length, 'tasks.'); + for (let i = 0; i < tasks.length; i++) { + console.log(i + 1, tasks[i]); + } + } +} + +function clearTasks() { + fs.writeFile(targetFile, defaultContents ,(err) => { + console.log(err); + }); + console.log(`Cleared up the tasks. (${tasks.length} tasks)`); +} + +function addTask() { + let task = process.argv.splice(3).join(' '); + tasks.push(task); + fs.writeFile(targetFile, JSON.stringify(tasks), (err) => { + console.log(err); + }) + console.log(`Added the given task. (${tasks.length} tasks)`); +} + +function help() { + console.log('-------------------- HOW TO USE --------------------'); + console.log('$ node tasks.js list: list up all the tasks.'); + console.log('$ node tasks.js clear: clear up all the tasks.'); + console.log('$ node tasks.js add : add the given task. '); + console.log('----------------------------------------------------'); +} diff --git a/lesson3/challenges/tasks.txt b/lesson3/challenges/tasks.txt new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/lesson3/challenges/tasks.txt @@ -0,0 +1 @@ +[] \ No newline at end of file