Basic Task Management Project(node js)

ยท

3 min read

In this post we are going to make a small beginner friendly project that is task management with node js readline-sync library.

The readline-sync library is commonly used in Node.js for synchronous reading of user input from the command line. It provides a simple way to interact with the user by waiting for their input and returning it to your program. This is especially useful for command-line interfaces (CLI) where you need to prompt the user for information.

for this project you will have to know about loop,switch case, arrays and basics of function(input / output ans calling)

Steps 1 . Make a Database

Steps 2 . Create a function for taking input from user

Steps 2.1 . function like addTask, ViewTask, updateTask and deleteTask.

Step 3 . Make a switch case for user choice.

The readline-sync library provides several methods for synchronous input in a command-line interface. Here are some of the commonly used methods:

readline.question(prompt): Asks the user a question, displaying the provided prompt. It returns the user's input as a string.

const readline = require('readline-sync');
const userInput = readline.question('Enter something: ');
console.log(`You entered: ${userInput}`);

readline.questionInt(prompt): Similar to readline.question, but specifically converts the user's input to an integer.

const readline = require('readline-sync');
const userNumber = readline.questionInt('Enter a number: ');
console.log(`You entered: ${userNumber}`);

Project code:

const readline = require('readline-sync');

// database
let tasks =[];


// taking user input
addTask = ()=>{
 const task = readline.question('Enter the task ');
 tasks.push(task);
 console.log(" ")
 console.log('Task is added Successfully!');
}


// funtion to view the task;
viewTsak = () =>{
    if(tasks.length === 0){
      console.log("No tak is found ");
    }
    else{
      console.log(" ")
      console.log("Number of Tasks ");
      console.log(" ")
      tasks.forEach((task,index)=>{
        console.log(`${index + 1}. ${task}`);
      });
    }
}


// Funciton to update task 
updateTask = ()=>{
 viewTsak(); 

  const index = readline.questionInt("Enter the task Number to update ")
  if(index>= 1 && index <= tasks.length){
      const updatedTask = readline.question("Enter the updated Task: ");
      tasks[index-1] = updatedTask;

      console.log("Tak is updated Succefully! ");
  }
  else{
    console.log("Invalid Task");
  }
}

// funciton to delete the task 

deleteTask = () =>{
  viewTsak();
  const index = readline.questionInt("Ente the Task Number to delete: ")

  if(index>= 1 && index<= tasks.length){
    tasks.slice(index -1,1);
//  index - 1 is used as the starting index to delete the task
// The second parameter, 1, specifies that we want to remove only one element at the specified index. 

console.log("Task is deleted Successfully");       
  }else{
    console.log("Invalid Task Number ");
  }
}

// main function

main =()=>{

  let exit = false;
  while(!exit){
    console.log("\nTask Management Application");
    console.log("1. Add Task")
    console.log("2. View Task")
    console.log("3. Update Task")
    console.log("4. Delete Task")
    console.log('5. Exit');
    console.log(" ")
    const choice = readline.questionInt("Enter the choice ")

    switch(choice){
      case 1:
        addTask();
        break;

      case 2:
        viewTsak();
        break;

      case 3:
        updateTask();
        break;

      case 4:
        deleteTask();
        break;

      case 5:
        console.log("Exiting...");
        exit = true;
        break;
      default:
        console.log("Invalid choce. Please try agin ")
    }
  }
}

// Start the application 
main();

The slice method in JavaScript doesn't modify the original array in place; instead, it returns a new array containing the selected elements.

tasks.splice(index - 1, 1)

index - 1: This is the starting index in the array from which elements will be removed. In JavaScript arrays, indices start from 0. Since the user is likely providing a task number starting from 1, we subtract 1 to get the correct array index.

: This is the number of elements to remove from the array starting at the specified index. In this case, it's set to 1, meaning it will remove one element.

Thank you for reading my content. Be sure to follow and comment on what you want me to write about next ๐Ÿค“

Did you find this article valuable?

Support Saurabh verma by becoming a sponsor. Any amount is appreciated!

ย