From 35535db5dafb024bd2d9e762ed75e9a0fc6df3ef Mon Sep 17 00:00:00 2001 From: eric-lee Date: Sat, 1 Oct 2022 01:38:52 -0700 Subject: [PATCH 1/6] initial_commit_hw3 --- todo_cli/todo_cli.js | 51 +++++ todo_cli/todo_cliV2.js | 90 +++++++++ .../turtle-graphics-including-stretch.js | 180 ++++++++++++++++++ 3 files changed, 321 insertions(+) create mode 100644 todo_cli/todo_cli.js create mode 100644 todo_cli/todo_cliV2.js create mode 100644 turtle_graphics/turtle-graphics-including-stretch.js diff --git a/todo_cli/todo_cli.js b/todo_cli/todo_cli.js new file mode 100644 index 0000000..b38e0a1 --- /dev/null +++ b/todo_cli/todo_cli.js @@ -0,0 +1,51 @@ +const readline = require("readline"); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +let todo_list = [ + ["0[] Take out the trash"], + ["1[] Buy toothpaste"], + ["2[] Buy Snickerdoodles"], + ["3[] Fix the climate"], + ["4[] Find a cure for again"], +]; + +function view() { + for (const todo of todo_list) { + console.log(todo.toString()); + } + // console.log(rl.getPrompt()); +} + +function add() { + console.log("What?"); + rl.on("line", (task) => { + if (task != "") { + todo_list.push(`${todo_list.length}[] ${task}`); + start(); + } + }); +} +function start() { + rl.setPrompt(`(v) View ● (n) New ● (cX) Complete ● (dX) Delete ● (q) Quit`); + console.log(rl.getPrompt()); + + rl.on("line", (input) => { + if (input === "j") { + view(); + start(); + } + if (input === "n") { + add(); + } + if (input === "q") { + console.log("See you soon!"); + rl.close(); + } + }); +} + +start(); diff --git a/todo_cli/todo_cliV2.js b/todo_cli/todo_cliV2.js new file mode 100644 index 0000000..a41a507 --- /dev/null +++ b/todo_cli/todo_cliV2.js @@ -0,0 +1,90 @@ +const readline = require("readline"); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +// let todo_list = [ +// ["0[] Take out the trash"], +// ["1[] Buy toothpaste"], +// ["2[] Buy Snickerdoodles"], +// ["3[] Fix the climate"], +// ["4[] Find a cure for again"], +// ]; +let todo_list = []; + +function view() { + if (todo_list.length > 0) { + for (const todo of todo_list) { + console.log(todo.toString()); + } + } else { + console.log("List is empty..."); + } + todo_function(); +} + +function add() { + rl.question("What? \n", (input) => { + if (input != "") { + todo_list.push(`${todo_list.length}[] ${input}`); + } + todo_function(); + }); +} + +function complete(index) { + if (index < todo_list.length) { + let a = todo_list[index].toString(); + let start = a.indexOf(" "); + console.log(`Completed "${a.slice(start + 1)}"`); + todo_list[index] = todo_list[index].toString().replace("[]", `[✔]`); + todo_function(); + } else { + console.log("Index out of range."); + todo_function(); + } +} + +function deleting(index) { + let a = todo_list[index].toString(); + let start = a.indexOf(" "); + console.log(`Deleted "${a.slice(start + 1)}"`); + todo_list.splice(index, 1); + for (let i = index; i < todo_list.length; i++) { + let a = todo_list[i].toString(); + let start = a.indexOf("["); + todo_list[i] = `${i}${a.slice(start)}`; + } + todo_function(); +} + +// rl.setPrompt(`(v) View ● (n) New ● (cX) Complete ● (dX) Delete ● (q) Quit`); +// console.log(rl.getPrompt()); + +function todo_function() { + rl.question( + "(v) View ● (n) New ● (cX) Complete ● (dX) Delete ● (q) Quit \n", + (input) => { + if (input === "v") { + view(); + } else if (input === "n") { + add(); + } else if (input[0] === "c") { + let addIndex = parseInt(input.slice(1)); + complete(addIndex); + } else if (input[0] === "d") { + let deleteIndex = parseInt(input.slice(1)); + deleting(deleteIndex); + } else if (input === "q") { + console.log("See you soon! 😄"); + rl.close(); + } else { + todo_function(); + } + } + ); +} + +todo_function(); diff --git a/turtle_graphics/turtle-graphics-including-stretch.js b/turtle_graphics/turtle-graphics-including-stretch.js new file mode 100644 index 0000000..1f03241 --- /dev/null +++ b/turtle_graphics/turtle-graphics-including-stretch.js @@ -0,0 +1,180 @@ +const fs = require("fs"); + +class Turtle { + constructor(x, y) { + this.x = x; + this.y = y; + this.arr = []; + this.rights = 0; + this.lefts = 0; + this.arr.push([this.x, this.y]); + this.str = ""; + this.maxY = 0; + this.maxX = 0; + return this; + } + forward(step) { + if (this.rights == 0 && this.lefts == 0) { + for (let i = 0; i < step; i++) { + this.x += 1; + this.arr.push([this.x, this.y]); + } + } + if (this.rights == 1 && this.lefts == 0) { + for (let i = 0; i < step; i++) { + this.y += 1; + this.arr.push([this.x, this.y]); + } + } + + if (this.lefts == 1 && this.rights == 0) { + for (let i = 0; i < step; i++) { + this.y -= 1; + this.arr.push([this.x, this.y]); + } + } + if (this.lefts == 1 && this.rights == 1) { + for (let i = 0; i < step; i++) { + this.x -= 1; + this.arr.push([this.x, this.y]); + } + } + + return this; + } + right() { + if (this.rights == 1 && this.lefts == 1) { + this.rights -= 1; + } else if (this.rights == 0 && this.lefts == 1) { + this.lefts -= 1; + } else if (this.rights == 1 && this.lefts == 0) { + this.lefts += 1; + } else { + this.rights += 1; + } + + return this; + } + left() { + if (this.lefts == 1 && this.rights == 1) { + this.lefts -= 1; + } else if (this.lefts == 1 && this.rights == 0) { + this.rights += 1; + } else if (this.lefts == 0 && this.rights == 1) { + this.rights -= 1; + } else { + this.lefts += 1; + } + + return this; + } + allPoints() { + return this.arr; + } + print() { + this.maxX = this.arr.sort((a, b) => a[0] - b[0]); + this.maxX = this.arr[this.arr.length - 1][0]; + this.maxY = this.arr.sort((a, b) => a[1] - b[1]); + this.maxY = this.arr[this.arr.length - 1][1]; + this.str += "--BEGIN LOG \n"; + for (let y = 0; y <= this.maxY; y++) { + for (let x = 0; x <= this.maxX + 1; x++) { + if (this.arr.find((element) => element[0] === x && element[1] === y)) { + this.str += "●"; + } else { + this.str += "○"; + } + } + this.str += "\n"; + } + this.str += "--END LOG"; + return this.str; + } +} + +// Function to split the script input into an array +// and run the script using the values +function runTurtleScript(input) { + let inputArr = input.split("-"); + let a; + + if (inputArr[0][0] == "t") { + a = new Turtle( + parseInt(inputArr[0].slice(1, inputArr[0].indexOf(","))), + parseInt(inputArr[0].slice(inputArr[0].indexOf(",") + 1)) + ); + } else { + a = new Turtle(0, 0); + } + for (let i = 0; i < inputArr.length; i++) { + if (inputArr[i][0] == "f") { + a.forward(parseInt(inputArr[i].slice(1))); + } + if (inputArr[i][0] == "r") { + a.right(); + } + if (inputArr[i][0] == "l") { + a.left(); + } + } + return a.print(); +} + +//Function to check if options were passed in, +//and run scripts accordingly +function scriptWithOption(input) { + // If there are no input + if (input == undefined) { + return; + } + // If the proper option were passed in + else if (input.slice(0, 9) == "--output=") { + let realInput = process.argv[3]; + let data = runTurtleScript(realInput); + + return fs.writeFile(input.slice(9), data, (err) => { + if (err) { + console.log(err); + } else { + console.log("🐢 Drawing written to " + input.slice(9)); + } + }); + } // if there are no option passed in, + // and the proper input were passed in, + // then run the script normally + else if ( + input[0][0] == "f" || + input[0][0] == "t" || + input[0][0] == "l" || + input[0][0] == "r" + ) { + return console.log(runTurtleScript(input)); + } // if nothing from the above passes, there must be a typo, so return this + else { + return console.log( + "Please check your input again. There may be a typographical error" + ); + } +} +// Below to run it as a script +let input = process.argv[2]; +scriptWithOption(input); + +// below to run it normally +// const result = new Turtle(0, 4) +// .forward(3) +// .left() +// .forward(3) +// .right() +// .forward(5) +// .right() +// .forward(8) +// .right() +// .forward(5) +// .right() +// .forward(3) +// .left() +// .forward(3) +// .print(); + +// console.log(result); From ada596b5f855b6952320b80f8b632d1fb33daabd Mon Sep 17 00:00:00 2001 From: eric-lee Date: Sat, 1 Oct 2022 01:41:23 -0700 Subject: [PATCH 2/6] deleted turtle directory --- .../turtle-graphics-including-stretch.js | 180 ------------------ 1 file changed, 180 deletions(-) delete mode 100644 turtle_graphics/turtle-graphics-including-stretch.js diff --git a/turtle_graphics/turtle-graphics-including-stretch.js b/turtle_graphics/turtle-graphics-including-stretch.js deleted file mode 100644 index 1f03241..0000000 --- a/turtle_graphics/turtle-graphics-including-stretch.js +++ /dev/null @@ -1,180 +0,0 @@ -const fs = require("fs"); - -class Turtle { - constructor(x, y) { - this.x = x; - this.y = y; - this.arr = []; - this.rights = 0; - this.lefts = 0; - this.arr.push([this.x, this.y]); - this.str = ""; - this.maxY = 0; - this.maxX = 0; - return this; - } - forward(step) { - if (this.rights == 0 && this.lefts == 0) { - for (let i = 0; i < step; i++) { - this.x += 1; - this.arr.push([this.x, this.y]); - } - } - if (this.rights == 1 && this.lefts == 0) { - for (let i = 0; i < step; i++) { - this.y += 1; - this.arr.push([this.x, this.y]); - } - } - - if (this.lefts == 1 && this.rights == 0) { - for (let i = 0; i < step; i++) { - this.y -= 1; - this.arr.push([this.x, this.y]); - } - } - if (this.lefts == 1 && this.rights == 1) { - for (let i = 0; i < step; i++) { - this.x -= 1; - this.arr.push([this.x, this.y]); - } - } - - return this; - } - right() { - if (this.rights == 1 && this.lefts == 1) { - this.rights -= 1; - } else if (this.rights == 0 && this.lefts == 1) { - this.lefts -= 1; - } else if (this.rights == 1 && this.lefts == 0) { - this.lefts += 1; - } else { - this.rights += 1; - } - - return this; - } - left() { - if (this.lefts == 1 && this.rights == 1) { - this.lefts -= 1; - } else if (this.lefts == 1 && this.rights == 0) { - this.rights += 1; - } else if (this.lefts == 0 && this.rights == 1) { - this.rights -= 1; - } else { - this.lefts += 1; - } - - return this; - } - allPoints() { - return this.arr; - } - print() { - this.maxX = this.arr.sort((a, b) => a[0] - b[0]); - this.maxX = this.arr[this.arr.length - 1][0]; - this.maxY = this.arr.sort((a, b) => a[1] - b[1]); - this.maxY = this.arr[this.arr.length - 1][1]; - this.str += "--BEGIN LOG \n"; - for (let y = 0; y <= this.maxY; y++) { - for (let x = 0; x <= this.maxX + 1; x++) { - if (this.arr.find((element) => element[0] === x && element[1] === y)) { - this.str += "●"; - } else { - this.str += "○"; - } - } - this.str += "\n"; - } - this.str += "--END LOG"; - return this.str; - } -} - -// Function to split the script input into an array -// and run the script using the values -function runTurtleScript(input) { - let inputArr = input.split("-"); - let a; - - if (inputArr[0][0] == "t") { - a = new Turtle( - parseInt(inputArr[0].slice(1, inputArr[0].indexOf(","))), - parseInt(inputArr[0].slice(inputArr[0].indexOf(",") + 1)) - ); - } else { - a = new Turtle(0, 0); - } - for (let i = 0; i < inputArr.length; i++) { - if (inputArr[i][0] == "f") { - a.forward(parseInt(inputArr[i].slice(1))); - } - if (inputArr[i][0] == "r") { - a.right(); - } - if (inputArr[i][0] == "l") { - a.left(); - } - } - return a.print(); -} - -//Function to check if options were passed in, -//and run scripts accordingly -function scriptWithOption(input) { - // If there are no input - if (input == undefined) { - return; - } - // If the proper option were passed in - else if (input.slice(0, 9) == "--output=") { - let realInput = process.argv[3]; - let data = runTurtleScript(realInput); - - return fs.writeFile(input.slice(9), data, (err) => { - if (err) { - console.log(err); - } else { - console.log("🐢 Drawing written to " + input.slice(9)); - } - }); - } // if there are no option passed in, - // and the proper input were passed in, - // then run the script normally - else if ( - input[0][0] == "f" || - input[0][0] == "t" || - input[0][0] == "l" || - input[0][0] == "r" - ) { - return console.log(runTurtleScript(input)); - } // if nothing from the above passes, there must be a typo, so return this - else { - return console.log( - "Please check your input again. There may be a typographical error" - ); - } -} -// Below to run it as a script -let input = process.argv[2]; -scriptWithOption(input); - -// below to run it normally -// const result = new Turtle(0, 4) -// .forward(3) -// .left() -// .forward(3) -// .right() -// .forward(5) -// .right() -// .forward(8) -// .right() -// .forward(5) -// .right() -// .forward(3) -// .left() -// .forward(3) -// .print(); - -// console.log(result); From 703b2e8df3ddde67d919c24f9f37a1f5516022c8 Mon Sep 17 00:00:00 2001 From: eric-lee Date: Sat, 1 Oct 2022 01:44:30 -0700 Subject: [PATCH 3/6] added_safeguard_to_delete_function --- todo_cli/todo_cliV2.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/todo_cli/todo_cliV2.js b/todo_cli/todo_cliV2.js index a41a507..99454d3 100644 --- a/todo_cli/todo_cliV2.js +++ b/todo_cli/todo_cliV2.js @@ -48,14 +48,18 @@ function complete(index) { } function deleting(index) { - let a = todo_list[index].toString(); - let start = a.indexOf(" "); - console.log(`Deleted "${a.slice(start + 1)}"`); - todo_list.splice(index, 1); - for (let i = index; i < todo_list.length; i++) { - let a = todo_list[i].toString(); - let start = a.indexOf("["); - todo_list[i] = `${i}${a.slice(start)}`; + if (index < todo_list.length) { + let a = todo_list[index].toString(); + let start = a.indexOf(" "); + console.log(`Deleted "${a.slice(start + 1)}"`); + todo_list.splice(index, 1); + for (let i = index; i < todo_list.length; i++) { + let a = todo_list[i].toString(); + let start = a.indexOf("["); + todo_list[i] = `${i}${a.slice(start)}`; + } + } else { + console.log("Index out of range."); } todo_function(); } From b2244e7925748a2dea5d5183afa24bbad1c7961a Mon Sep 17 00:00:00 2001 From: eric-lee Date: Sun, 2 Oct 2022 05:28:47 -0700 Subject: [PATCH 4/6] added strecth --- todo_cli/todo_cli.js | 51 ------------- todo_cli/todo_stretch.js | 156 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+), 51 deletions(-) delete mode 100644 todo_cli/todo_cli.js create mode 100644 todo_cli/todo_stretch.js diff --git a/todo_cli/todo_cli.js b/todo_cli/todo_cli.js deleted file mode 100644 index b38e0a1..0000000 --- a/todo_cli/todo_cli.js +++ /dev/null @@ -1,51 +0,0 @@ -const readline = require("readline"); - -const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout, -}); - -let todo_list = [ - ["0[] Take out the trash"], - ["1[] Buy toothpaste"], - ["2[] Buy Snickerdoodles"], - ["3[] Fix the climate"], - ["4[] Find a cure for again"], -]; - -function view() { - for (const todo of todo_list) { - console.log(todo.toString()); - } - // console.log(rl.getPrompt()); -} - -function add() { - console.log("What?"); - rl.on("line", (task) => { - if (task != "") { - todo_list.push(`${todo_list.length}[] ${task}`); - start(); - } - }); -} -function start() { - rl.setPrompt(`(v) View ● (n) New ● (cX) Complete ● (dX) Delete ● (q) Quit`); - console.log(rl.getPrompt()); - - rl.on("line", (input) => { - if (input === "j") { - view(); - start(); - } - if (input === "n") { - add(); - } - if (input === "q") { - console.log("See you soon!"); - rl.close(); - } - }); -} - -start(); diff --git a/todo_cli/todo_stretch.js b/todo_cli/todo_stretch.js new file mode 100644 index 0000000..b2aac2e --- /dev/null +++ b/todo_cli/todo_stretch.js @@ -0,0 +1,156 @@ +const fs = require("fs"); +const readline = require("readline"); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +let todo_list = []; + +const file = process.argv[2]; + +//Reading json file if "json" exist in user input and adding the data to todo_list +if (file != undefined && file.includes(".json")) { + fs.readFile(file, "utf8", (err, data) => { + if (err) { + console.error; + } else { + let words = JSON.parse(data); + words.forEach((element, index) => { + if (element.completed == true) { + todo_list.push(`${index}[✔] ${element.title}`); + } else { + todo_list.push(`${index}[] ${element.title}`); + } + }); + } + }); +} + +function view() { + if (todo_list.length > 0) { + for (const todo of todo_list) { + console.log(todo.toString()); + } + } else { + console.log("List is empty..."); + } + todo_function(); +} + +function add() { + rl.question("What? \n", (input) => { + if (input != "") { + todo_list.push(`${todo_list.length}[] ${input}`); + } + todo_function(); + }); +} + +function complete(index) { + if (index < todo_list.length) { + let a = todo_list[index].toString(); + let start = a.indexOf(" "); + console.log(`Completed "${a.slice(start + 1)}"`); + todo_list[index] = todo_list[index].toString().replace("[]", `[✔]`); + todo_function(); + } else { + console.log("Index out of range."); + todo_function(); + } +} + +function deleting(index) { + if (index < todo_list.length) { + let a = todo_list[index].toString(); + let start = a.indexOf(" "); + console.log(`Deleted "${a.slice(start + 1)}"`); + todo_list.splice(index, 1); + for (let i = index; i < todo_list.length; i++) { + let a = todo_list[i].toString(); + let start = a.indexOf("["); + todo_list[i] = `${i}${a.slice(start)}`; + } + } else { + console.log("Index out of range."); + } + todo_function(); +} + +// rl.setPrompt(`(v) View ● (n) New ● (cX) Complete ● (dX) Delete ● (q) Quit`); +// console.log(rl.getPrompt()); + +function todo_function() { + rl.question( + "(v) View ● (n) New ● (cX) Complete ● (dX) Delete ● (s) Save ● (q) Quit \n", + (input) => { + if (input === "v") { + view(); + } else if (input === "n") { + add(); + } else if (input[0] === "c") { + let addIndex = parseInt(input.slice(1)); + complete(addIndex); + } else if (input[0] === "d") { + let deleteIndex = parseInt(input.slice(1)); + deleting(deleteIndex); + } else if (input === "q") { + console.log("See you soon! 😄"); + rl.close(); + } else if (input === "s") { + save(); + } else { + todo_function(); + } + } + ); +} + +function todo_list_To_JSON() { + let result = []; + + for (const todo of todo_list) { + let startIndex = todo.indexOf(" "); + + if (todo.includes("[✔]")) { + let obj = { completed: true, title: todo.slice(startIndex + 1) }; + let todoJSON = JSON.stringify(obj); + result.push(todoJSON); + } else { + let obj = { completed: false, title: todo.slice(startIndex + 1) }; + let todoJSON = JSON.stringify(obj); + result.push(todoJSON); + } + } + return result; +} +function save() { + let result = todo_list_To_JSON(); + + rl.question("What? (myTodos.json)\n", (input) => { + if (input.includes(".json")) { + fs.writeFile(input, `[${result}]`, "utf8", (err) => { + if (err) { + console.error; + } else { + console.log(`List saved to "${input}"`); + todo_function(); + } + }); + } else { + fs.writeFile("myTodos.json", `[${result}]`, "utf8", (err) => { + if (err) { + console.error; + } else { + console.log(`List saved to "myTodos.json"`); + todo_function(); + } + }); + } + }); +} + +console.log("\nWelcome to Todo CLI!"); +console.log("---------------------"); + +todo_function(); From 116a75d53ed09b8d6eea3152eac09afd7da5ece9 Mon Sep 17 00:00:00 2001 From: eric-lee Date: Sun, 2 Oct 2022 05:37:27 -0700 Subject: [PATCH 5/6] minor changes to files --- todo_cli/myTodos.json | 6 ++ todo_cli/{todo_cliV2.js => todo_cli.js} | 4 +- .../{todo_stretch.js => todo_cli_stretch.js} | 60 ++++++++++--------- 3 files changed, 39 insertions(+), 31 deletions(-) create mode 100644 todo_cli/myTodos.json rename todo_cli/{todo_cliV2.js => todo_cli.js} (96%) rename todo_cli/{todo_stretch.js => todo_cli_stretch.js} (95%) diff --git a/todo_cli/myTodos.json b/todo_cli/myTodos.json new file mode 100644 index 0000000..84d452c --- /dev/null +++ b/todo_cli/myTodos.json @@ -0,0 +1,6 @@ +[ + { "completed": true, "title": "Feed the raptops" }, + { "completed": false, "title": "Have lunch" }, + { "completed": false, "title": "Go see Deadpool 2" }, + { "completed": false, "title": "Write a script to fix the climate" } +] diff --git a/todo_cli/todo_cliV2.js b/todo_cli/todo_cli.js similarity index 96% rename from todo_cli/todo_cliV2.js rename to todo_cli/todo_cli.js index 99454d3..44cc112 100644 --- a/todo_cli/todo_cliV2.js +++ b/todo_cli/todo_cli.js @@ -76,8 +76,8 @@ function todo_function() { } else if (input === "n") { add(); } else if (input[0] === "c") { - let addIndex = parseInt(input.slice(1)); - complete(addIndex); + let completeIndex = parseInt(input.slice(1)); + complete(completeIndex); } else if (input[0] === "d") { let deleteIndex = parseInt(input.slice(1)); deleting(deleteIndex); diff --git a/todo_cli/todo_stretch.js b/todo_cli/todo_cli_stretch.js similarity index 95% rename from todo_cli/todo_stretch.js rename to todo_cli/todo_cli_stretch.js index b2aac2e..c77cb10 100644 --- a/todo_cli/todo_stretch.js +++ b/todo_cli/todo_cli_stretch.js @@ -77,35 +77,7 @@ function deleting(index) { todo_function(); } -// rl.setPrompt(`(v) View ● (n) New ● (cX) Complete ● (dX) Delete ● (q) Quit`); -// console.log(rl.getPrompt()); - -function todo_function() { - rl.question( - "(v) View ● (n) New ● (cX) Complete ● (dX) Delete ● (s) Save ● (q) Quit \n", - (input) => { - if (input === "v") { - view(); - } else if (input === "n") { - add(); - } else if (input[0] === "c") { - let addIndex = parseInt(input.slice(1)); - complete(addIndex); - } else if (input[0] === "d") { - let deleteIndex = parseInt(input.slice(1)); - deleting(deleteIndex); - } else if (input === "q") { - console.log("See you soon! 😄"); - rl.close(); - } else if (input === "s") { - save(); - } else { - todo_function(); - } - } - ); -} - +//converting todo_list to JSON function todo_list_To_JSON() { let result = []; @@ -124,10 +96,12 @@ function todo_list_To_JSON() { } return result; } + function save() { let result = todo_list_To_JSON(); rl.question("What? (myTodos.json)\n", (input) => { + // if user inputs path, save data to that path if (input.includes(".json")) { fs.writeFile(input, `[${result}]`, "utf8", (err) => { if (err) { @@ -138,6 +112,7 @@ function save() { } }); } else { + // if user does not pass in path, save to default path "myTodos.json" fs.writeFile("myTodos.json", `[${result}]`, "utf8", (err) => { if (err) { console.error; @@ -150,6 +125,33 @@ function save() { }); } +// created todo_function to do recurssion +function todo_function() { + rl.question( + "(v) View ● (n) New ● (cX) Complete ● (dX) Delete ● (s) Save ● (q) Quit \n", + (input) => { + if (input === "v") { + view(); + } else if (input === "n") { + add(); + } else if (input[0] === "c") { + let addIndex = parseInt(input.slice(1)); + complete(addIndex); + } else if (input[0] === "d") { + let deleteIndex = parseInt(input.slice(1)); + deleting(deleteIndex); + } else if (input === "q") { + console.log("See you soon! 😄"); + rl.close(); + } else if (input === "s") { + save(); + } else { + todo_function(); + } + } + ); +} + console.log("\nWelcome to Todo CLI!"); console.log("---------------------"); From 85bac83a4b365a4e45ba1886a74b1a306d4f7acf Mon Sep 17 00:00:00 2001 From: eric-lee Date: Thu, 6 Oct 2022 22:31:06 -0700 Subject: [PATCH 6/6] deleted one file --- todo_cli/todo_cli.js | 94 -------------------------------------------- 1 file changed, 94 deletions(-) delete mode 100644 todo_cli/todo_cli.js diff --git a/todo_cli/todo_cli.js b/todo_cli/todo_cli.js deleted file mode 100644 index 44cc112..0000000 --- a/todo_cli/todo_cli.js +++ /dev/null @@ -1,94 +0,0 @@ -const readline = require("readline"); - -const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout, -}); - -// let todo_list = [ -// ["0[] Take out the trash"], -// ["1[] Buy toothpaste"], -// ["2[] Buy Snickerdoodles"], -// ["3[] Fix the climate"], -// ["4[] Find a cure for again"], -// ]; -let todo_list = []; - -function view() { - if (todo_list.length > 0) { - for (const todo of todo_list) { - console.log(todo.toString()); - } - } else { - console.log("List is empty..."); - } - todo_function(); -} - -function add() { - rl.question("What? \n", (input) => { - if (input != "") { - todo_list.push(`${todo_list.length}[] ${input}`); - } - todo_function(); - }); -} - -function complete(index) { - if (index < todo_list.length) { - let a = todo_list[index].toString(); - let start = a.indexOf(" "); - console.log(`Completed "${a.slice(start + 1)}"`); - todo_list[index] = todo_list[index].toString().replace("[]", `[✔]`); - todo_function(); - } else { - console.log("Index out of range."); - todo_function(); - } -} - -function deleting(index) { - if (index < todo_list.length) { - let a = todo_list[index].toString(); - let start = a.indexOf(" "); - console.log(`Deleted "${a.slice(start + 1)}"`); - todo_list.splice(index, 1); - for (let i = index; i < todo_list.length; i++) { - let a = todo_list[i].toString(); - let start = a.indexOf("["); - todo_list[i] = `${i}${a.slice(start)}`; - } - } else { - console.log("Index out of range."); - } - todo_function(); -} - -// rl.setPrompt(`(v) View ● (n) New ● (cX) Complete ● (dX) Delete ● (q) Quit`); -// console.log(rl.getPrompt()); - -function todo_function() { - rl.question( - "(v) View ● (n) New ● (cX) Complete ● (dX) Delete ● (q) Quit \n", - (input) => { - if (input === "v") { - view(); - } else if (input === "n") { - add(); - } else if (input[0] === "c") { - let completeIndex = parseInt(input.slice(1)); - complete(completeIndex); - } else if (input[0] === "d") { - let deleteIndex = parseInt(input.slice(1)); - deleting(deleteIndex); - } else if (input === "q") { - console.log("See you soon! 😄"); - rl.close(); - } else { - todo_function(); - } - } - ); -} - -todo_function();