Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e20f640

Browse files
committed
added homework for week 2
1 parent 4d6451f commit e20f640

File tree

7 files changed

+183
-0
lines changed

7 files changed

+183
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
*/.DS_Store

week1/repeated-mistakes.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Some examples of last week
2+
// 1: Do not forget to assign a new number when adding / subtracting
3+
4+
var state = 0;
5+
6+
console.log('original state is', state);
7+
console.log('plus one state is', state + 1);
8+
console.log('plus another one state is', state + 1);
9+
10+
// ---
11+
// 2: Be stylish!
12+
13+
// Watch spaces and tabs:
14+
// Bad: if (request.url==='/' || request.url==='/state' || request.url==='/reset'){
15+
// Good: if (request.url === '/' || request.url === '/state' || request.url === '/reset') {
16+
17+
// ---
18+
// 3: Think and write DRY: Do Not Repeat Yourself
19+

week2/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## Summary:
2+
Last week we looked at building an HTTP interface. The interface allowed us to get a state, and manipulate the state (add, subtract, reset). This week's key topics are:
3+
1. Persisting data beyond the lifetime of the app.
4+
1. Building a Command Line Interface
5+
1. Using Node.JS's FileSystem (FS)
6+
7+
## Assignment for this week
8+
These are the specs for this week's assignment:
9+
- The user can run a NodeJs to-do app
10+
- The user can be able to run the file using node index.js
11+
- There should be a "help" section that lists all the commands for how to use the app
12+
13+
The following commands should be present:
14+
- No command: show help section
15+
- help: show help section
16+
- list: show current todo's, or show an appropriate text if there are no todos
17+
- add: add a todo item. all the words behind "add" are entered as 1 todo item to the list
18+
- remove: remove a todo item by its 1-base index.
19+
- reset: remove all todo items from the list
20+
21+
Chocolate cake for the person who can succeed the most tests :) (prices will have to be shared in case of equal score)
22+
23+
## Reading material
24+
25+
### Something about ES6 I want you guys to know
26+
Old guys like Joost, Erol and I are talking about this "ES6" all the time. ES6 basically means: the latest version of NodeJS. It's a lot of really nice new features. For you guys, you should remember the following
27+
> During the NodeJS course, we will teach you some ES6 features, like Fat Arrow. It's *extremely* important to know whether a function comes from ES6 or from an older version of JavaScript. Why? [Because browsers don't support every new feature just yet](http://kangax.github.io/compat-table/es6/). NodeJS, on the other hand, you can always control which version of Javascript is running, because it's running on your computer, not in the browser. Version 6.x that you are running supports most ES6.
28+
29+
### 1. ES6: Fat Arrow functions
30+
This is one example of how ES6 can help us write cleaner code. I'm adding this as first reading material because it's used a lot on the NodeJS documentation website, so it's a good idea to understand what this means. Bonus points if you write your callbacks this way.
31+
[Blogpost Sitepoint]([https://www.sitepoint.com/es6-arrow-functions-new-fat-concise-syntax-javascript/)
32+
[Video]([https://www.youtube.com/watch?v=J85lRtO_yjY)
33+
34+
### 2. NodeJS Process:
35+
Don't have to remember everything in this video, just a nice outline
36+
[Egghead video tutorial](https://egghead.io/lessons/node-js-the-node-js-process-object)
37+
Only read the part about "process.argv"
38+
[Node.JS docs - process.argv](https://nodejs.org/docs/latest/api/process.html#process_process_argv)
39+
40+
### 3. NodeJS FS
41+
Only read the part about readFile, appendFile (you will need this in your assignment)
42+
[Node.JS docs - fs.readFile](https://nodejs.org/api/fs.html#fs_fs_readfile_file_options_callback)
43+
[Node.JS docs - fs.appendFile](https://nodejs.org/api/fs.html#fs_fs_appendfile_file_data_options_callback)
44+
45+
### 4. Buffers in NodeJS
46+
[Egghead video tutorial](https://egghead.io/lessons/node-js-node-js-buffers)

week2/help.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
This is the help page for CLI ToDo app
2+
3+
Instructions:
4+
5+
To display this set of instructions:
6+
node index.js help
7+
8+
To add an item to your todo's:
9+
e.g.
10+
node index.js add I need to brush my teeth
11+
12+
To list all your todo's
13+
e.g.
14+
node index.js list
15+
16+
To remove an item from your todo's
17+
First list, and find the location (index, 0 based) of your todo
18+
19+
To remove all items from your todo list
20+
node index.js reset
21+
22+
e.g.
23+
node index.js remove 0
24+
25+

week2/index.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var fs = require('fs');
2+
3+
var options = process.argv.slice(2);
4+
5+
var command = options[0];
6+
7+
switch (command) {
8+
case 'help':
9+
default:
10+
showHelp();
11+
break;
12+
case 'list':
13+
listTodos();
14+
break;
15+
}
16+
17+
function splitStringByNewline(string) {
18+
return string.split('\n').filter(function(element) {
19+
element = element.trim();
20+
return element.length > 0;
21+
});
22+
}
23+
24+
function showHelp() {
25+
openFile('help.txt', function(error, data) {
26+
if (error) {
27+
return console.log('Error: the help file could not be displayed', error);
28+
}
29+
console.log(data);
30+
});
31+
}
32+
33+
function listTodos() {
34+
openFile('todo.txt', function(error, data) {
35+
if (error) {
36+
if (error.code === 'ENOENT') {
37+
return console.log('Nothing to do! (or your dog ate your todo list)');
38+
} else {
39+
return console.log('Error: Something went wrong', error);
40+
}
41+
}
42+
43+
var todos = splitStringByNewline(data);
44+
45+
if (todos.length === 0) {
46+
return console.log('Nothing to do!')
47+
}
48+
49+
console.log('Your todo list looks like this');
50+
todos.forEach(function(element, index) {
51+
index = (index + 1).toString();
52+
console.log(index, element);
53+
});
54+
55+
if (todos.length > 5) {
56+
console.log('You have too much to do!');
57+
}
58+
});
59+
}
60+
61+
function openFile(fileName, callback) {
62+
fs.readFile(__dirname + '/' + fileName, 'utf8', function(error, data) {
63+
callback(error, data)
64+
});
65+
}

week2/package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "week2",
3+
"version": "1.0.0",
4+
"description": "week 2 example ",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/JonasBW/nodeJSEx1.git"
12+
},
13+
"keywords": [
14+
"nodejs",
15+
"filesystem"
16+
],
17+
"author": "Daan and Erol",
18+
"license": "ISC",
19+
"bugs": {
20+
"url": "https://github.com/JonasBW/nodeJSEx1/issues"
21+
},
22+
"homepage": "https://github.com/JonasBW/nodeJSEx1#readme"
23+
}

week2/todo.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
watch tv
2+
buy a house
3+
stuff like that

0 commit comments

Comments
 (0)