Skip to content
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
3 changes: 3 additions & 0 deletions week3/list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0 [ ] Take out the trash
1 [ ] Buy grocery
2 [ ] Play basketball
115 changes: 115 additions & 0 deletions week3/todoCLI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

let myStuff = [];
//The above "myStuff" array will be storing the todo's that a user enters.
function userInput(answer){

Choose a reason for hiding this comment

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

good job naming this function so that it could be reused

if (answer === 'v'){
viewList();
} else if (answer === 'n'){
addNewItem();
} else if (answer[0] === 'c') {
completeItem(answer);
} else if (answer[0] === 'd') {
deleteItem(answer);
} else if (answer === 'q'){
quitList();
} else {
console.log(`You must enter one of the following: v, n, cX (where X is a number), dX (where X is a number), q`);
menu();
};
};

function menu () {rl.question(
Copy link

@maxwellgordon maxwellgordon Dec 2, 2018

Choose a reason for hiding this comment

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

function menu () {rl.question(

should be on separate lines

function menu() {
  rl.question(

Choose a reason for hiding this comment

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

Good job naming this function so that it could be reused

`Welcome to ToDo CLI!\n--------------------\n(v) View ∙ (n) New ∙ (cX) Complete ∙ (dX) Delete ∙ (q) Quit\n>`,
userInput)};

Choose a reason for hiding this comment

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

should put the }; on a different line.

menu();

Choose a reason for hiding this comment

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

should not be indented if you are calling this now. Otherwise it gives the impression that this should be/currently is within another function


function viewList (){
Copy link

@maxwellgordon maxwellgordon Dec 2, 2018

Choose a reason for hiding this comment

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

When naming functions, please follow the following convention:

function functionName() {
    // ...
}

console.log(`You're viewing your to-do list`);
if (myStuff.length === 0) {
console.log('but your to-do list is empty')
} else {
for (let i = 0; i < myStuff.length; i++){
if (myStuff[i][0] === false) {
console.log (`${i} [ ] ${myStuff[i][1]}`)
} else if (myStuff[i][0] === true) {
console.log(`${i} [✓] ${myStuff[i][1]}`)
}
}
};
menu();
};

function addNewItem (){

Choose a reason for hiding this comment

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

console.log(`you're adding a new to-do item`);
rl.question(`What task would you like to add?\n`, answer =>{
myStuff.push([false, answer]);
menu();
});
};

function completeItem (answer) {
let userInputNumber = '';
for (let i = 1; i<answer.length; i++){
userInputNumber += answer[i]
};

Choose a reason for hiding this comment

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

This for loop essentially slices off the first character within the answer string.
Can also use

const userInputNumber = answer.slice(1);


myStuff[userInputNumber][0] = true;

Choose a reason for hiding this comment

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

you should check that userInputNumber is a valid number. What happens if there are only 2 items in the list myStuff but the user types in c5? Currently this causes an error and the app quits and breaks

console.log(`Completed "${myStuff[userInputNumber][1]}"`)
menu();
};

function deleteItem (answer) {

Choose a reason for hiding this comment

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

let userInputNumber = '';
for (let i = 1; i<answer.length; i++){
userInputNumber += answer[i]
};

let itemToDelete = myStuff.splice(userInputNumber,1)
Copy link

@maxwellgordon maxwellgordon Dec 2, 2018

Choose a reason for hiding this comment

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

same as #3 (comment)

console.log(`Deleted "${itemToDelete[0][1]}"`);
menu();
};

function quitList (){

Choose a reason for hiding this comment

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

console.log(`See you soon! 😄`);
rl.close();
};

//Nov 23, 2018
//The above is just to create a general/barebone structure
//of the code. This is just to build something, so that
//I can see the "main page" of todoCLI.js asking a user to
//choose from the v, n, cX, dX and q options.

/**
* MY PROPOSED ALGORITHM - Nov 23, 2018
* step 1 - create a general/barebone structure to show the
* interface as soon as todoCLI.js is run in the terminal.
* step 2 - In the callback function of rl.question(), call
* another function called "userInput".
* step 3 - In this "userInput" function, there will be various "if...else" statements
* responsible for the available choices.
* I expect to use the following methods for each option:
* view --> fs.readFile, rl.close()
* new --> fs.writeFile, rl.close()
* complete->fs.writeFile, rl.close()
* delete--> fs.unlink, rl.close()
* quit --> rl.close()
*/

/** Nov 25, 2018
* I realized I started coding for the "stretch" homework.
* So, I decided to rewrite my code for the "non-stretch" homework first.
* I will tackle the "stretch" homework later.
*
*
*
** Nov 30, 2018
* I cleaned up my code by deleting unneccesary comments and empty spaces.
* I cleaned up my code by indenting properly.
* I cleaned up my code by naming my variable with meaningful word choices.
*/