From bb21628582e75aa3ffa77f41b2f5b651e925efbc Mon Sep 17 00:00:00 2001 From: ohtoe02 Date: Fri, 8 Oct 2021 13:10:26 +0500 Subject: [PATCH 1/3] 1-4 --- index.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 8635050..e9e8302 100644 --- a/index.js +++ b/index.js @@ -12,14 +12,62 @@ function getFiles() { } function processCommand(command) { - switch (command) { + let words = command.split(' ') + switch (words[0]) { case 'exit': process.exit(0); break; + case 'show': + console.log(showTODOS()); + break; + case 'important': + console.log(showImportant()); + break; + case 'user': + console.log(showNames(words[1].toLowerCase())); + break; default: console.log('wrong command'); break; } } +function parseLines() { + let parsedFiles = []; + let lines = []; + for (let i = 0; i < files.length; i++) { + parsedFiles.push(files[i].split("\r\n")); + lines = lines.concat(parsedFiles[i]); + } + return lines; +} + +function showNames(username) { + let namedLines = []; + let lines = parseLines(); + for (let line of lines) + if (line.includes("// TODO") && line.toLowerCase().includes(username)) + namedLines.push(line.slice(line.indexOf("// TODO")).split(';')[2].replace(' ', '')); + return namedLines; +} + +function showImportant() { + let importantLines = []; + let lines = parseLines(); + for (let line of lines) + if (line.includes("// TODO") && line.includes("!")) + importantLines.push(line.slice(line.indexOf("// TODO"))); + return importantLines; +} + +function showTODOS() { + let toDoLines = []; + let lines = parseLines(); + for (let line of lines) + if (line.includes("// TODO")) + toDoLines.push(line.slice(line.indexOf("// TODO"))); + return toDoLines; +} + // TODO you can do it! + From 9914d03db331f256b946246c419939e0c3bf4837 Mon Sep 17 00:00:00 2001 From: ohtoe02 Date: Fri, 15 Oct 2021 01:13:59 +0500 Subject: [PATCH 2/3] 1-4 --- index.js | 57 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index e9e8302..c6f763d 100644 --- a/index.js +++ b/index.js @@ -26,6 +26,17 @@ function processCommand(command) { case 'user': console.log(showNames(words[1].toLowerCase())); break; + case 'sort': + switch (words[1]) { + case 'importance': + console.log(sortImportance()); + break; + case 'user': + break; + case 'date': + + } + break; default: console.log('wrong command'); break; @@ -42,32 +53,46 @@ function parseLines() { return lines; } +function showTODOS() { + let toDoLines = []; + let lines = parseLines(); + lines.forEach(line => {let index = line.indexOf("// TODO") + if (index !== -1 && line[index + 7] !== '"') + toDoLines.push(line.slice(index))}); + return toDoLines; +} + function showNames(username) { let namedLines = []; - let lines = parseLines(); - for (let line of lines) - if (line.includes("// TODO") && line.toLowerCase().includes(username)) - namedLines.push(line.slice(line.indexOf("// TODO")).split(';')[2].replace(' ', '')); + let todos = showTODOS(); + todos.forEach(line => {if (line.toLowerCase().includes(username)) + namedLines.push(line.slice(line.indexOf("// TODO")).split(';')[2].replace(' ', ''))}); return namedLines; } function showImportant() { let importantLines = []; - let lines = parseLines(); - for (let line of lines) - if (line.includes("// TODO") && line.includes("!")) - importantLines.push(line.slice(line.indexOf("// TODO"))); + let todos = showTODOS(); + todos.forEach(line => {if (line.includes("!")) importantLines.push(line)}); return importantLines; } -function showTODOS() { - let toDoLines = []; - let lines = parseLines(); - for (let line of lines) - if (line.includes("// TODO")) - toDoLines.push(line.slice(line.indexOf("// TODO"))); - return toDoLines; +function sortImportance() { + return showTODOS().sort((todo1, todo2) => todo2.split('!').length - todo1.split('!').length) } -// TODO you can do it! +function sortUser() { + return showTODOS().slice().sort((a, b) => { + let firstName = a.split(' ').slice(2).join(' '); + firstName = firstName.split(';')[0]; + let secondName = b.split(' ').slice(2).join(' '); + secondName = secondName.split(';')[0]; + if(firstName === a.split(' ').slice(2).join(' ') || secondName === b.split(' ').slice(2).join(' ')) + return 1 + + return firstName.length - secondName.length; + }) +} + +// TODO you can do it! From 3371174304b8209fae3d6334d40cadff545c394b Mon Sep 17 00:00:00 2001 From: ohtoe02 Date: Fri, 15 Oct 2021 14:12:08 +0500 Subject: [PATCH 3/3] 1-5 --- index.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index c6f763d..aac70ff 100644 --- a/index.js +++ b/index.js @@ -32,9 +32,11 @@ function processCommand(command) { console.log(sortImportance()); break; case 'user': + console.log(sortByUser()); break; case 'date': - + console.log(sortByDate()); + break; } break; default: @@ -81,18 +83,28 @@ function sortImportance() { return showTODOS().sort((todo1, todo2) => todo2.split('!').length - todo1.split('!').length) } -function sortUser() { - return showTODOS().slice().sort((a, b) => { - let firstName = a.split(' ').slice(2).join(' '); - firstName = firstName.split(';')[0]; - let secondName = b.split(' ').slice(2).join(' '); - secondName = secondName.split(';')[0]; +function sortByUser() { + return showTODOS().slice().sort((todo1, todo2) => { + let firstName = todo1.split(' ').slice(2).join(' ').split(';')[0]; + let secondName = todo2.split(' ').slice(2).join(' ').split(';')[0]; - if(firstName === a.split(' ').slice(2).join(' ') || secondName === b.split(' ').slice(2).join(' ')) + if(firstName === todo1.split(' ').slice(2).join(' ') || secondName === todo2.split(' ').slice(2).join(' ')) return 1 - return firstName.length - secondName.length; + return secondName.length - firstName.length; }) } +function sortByDate(){ + return showTODOS().sort((todo1, todo2) => { + todo1 = todo1.split(';'); + todo2 = todo2.split(';'); + if (todo1.length > 2 && todo2.length > 2) { + todo1 = todo1[1].split(' ').join('').split('-').join(''); + todo2 = todo2[1].split(' ').join('').split('-').join(''); + } + return todo2 - todo1; + }); +} + // TODO you can do it!