Skip to content

Instantly share code, notes, and snippets.

@muindetuva
Created December 13, 2023 17:30
Show Gist options
  • Select an option

  • Save muindetuva/59c19049e4f8cf19dc9e72ab4d8351cf to your computer and use it in GitHub Desktop.

Select an option

Save muindetuva/59c19049e4f8cf19dc9e72ab4d8351cf to your computer and use it in GitHub Desktop.
TodoApp
// Simple Todo Application
const readlineSync = require('readline-sync');
const { listTodos, markTodoCompleted, addTodo } = require('./todoFunctions')
function displayMenu() {
console.log('\n\nTodo Application Menu: 1. Add | 2. Mark Completed | 3. List | 4. Exit');
}
function runTodoApp(){
//
let choice;
do {
displayMenu();
// Fetch input
// Python code -> choice = input("Enter Choice (1 - 4)")
choice = readlineSync.question("Enter Choice (1-4): ")
// Perform action based on picked choice
switch (choice) {
case '1':
console.log("You want to add a task")
const todo = readlineSync.question("New Todo Item: ")
// Call the function for adding the item
addTodo(todo)
console.log("Todo Item Added successfully")
break;
case '2':
console.log("You want to mark a task completed")
const number = readlineSync.question("Mark as completed (Enter Todo number): ")
if(markTodoCompleted(number)) {
console.log(`Todo number ${number} is marked as completed`)
} else {
console.log("Invalid Todo number!!")
}
break;
case '3':
console.log("You want to list all tasks")
const todos = listTodos()
todos.forEach((todo, index) => {
console.log(`${index + 1}. ${todo.completed ? '[X]' : '[ ]'} ${todo.todo}`)
})
break;
case '4':
console.log("Exiting the App, Goodbye!");
break;
default:
console.log("Invalid choice. Pick between 1 and 4")
}
}
while (choice != 4)
}
// Run our application
runTodoApp();
// Todo Functions
let todos = [];
function addTodo(todo) {
todos.push({ todo, completed: false})
}
function markTodoCompleted(number) {
let index = number - 1
if (index >= 0 && index < todos.length){
todos[index].completed = true;
return true;
}
return false
}
function listTodos() {
return todos;
}
module.exports = {addTodo, markTodoCompleted, listTodos}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment