How to delete an input stored in array in JS

Viewed 126
let input;
const todos = [];

while (input !== 'exit') {
    input = prompt('Type what you want to do');
    if (input === 'new') {
        input = prompt("What's your todo?");
        todos.push(input);
    } else if (input === 'list') {
        console.log(`This is your list of todos: ${todos}`);
    } else if (input === 'delete') {
        input = prompt('Which todo would you like to delete?');
        if (todos.indexOf(input) !== -1) {
            todos.splice(1, 1, input);
            console.log(`You've deleted ${input}`);
        }
    } else {
        break;
    }
}

That's what I've tried so far. I'm starting into programing and this is part of a small exercise that I have to ask from a prompt to add a new todo, list it all and then delete. What i'm trying to do is: take that input stored at input variable and then check if its inside the array, if its positive I want to delete it and not from the index but from the word.

Like:

-delete -eat //check if its inside array //if true delete it from

I apologize if its a dumb question. I tried online and couldn't find it.

Thanks!

4 Answers

You could change the loop to a do while loop to check the exit instead of using break at last else of checking.

Then you need to store the result of indexOf and splice the item with the index.

let input;
const todos = [];

do {
  input = prompt('Type what you want to do');
  if (input === 'new') {
    input = prompt("What's your todo?");
    todos.push(input);
  } else if (input === 'list') {
    console.log(`This is your list of todos: ${todos}`);
  } else if (input === 'delete') {
    input = prompt('Which todo would you like to delete?');
    const index = todos.indexOf(input)
    if (index !== -1) {
      todos.splice(index, 1);
      console.log(`You've deleted ${input}`);
    }
  }
} while (input !== 'exit');

A slifhtly better approach is to take a switch statement.

let input;
const todos = [];

do {
    input = prompt('Type what you want to do');
    switch (input) {
        case 'new':
            input = prompt("What's your todo?");
            todos.push(input);
            break;
        case 'list':
            console.log(`This is your list of todos: ${todos}`);
            break;
        case 'delete':
            input = prompt('Which todo would you like to delete?');
            const index = todos.indexOf(input)
            if (index !== -1) {
                todos.splice(index, 1);
                console.log(`You've deleted ${input}`);
            }
    }
} while (input !== 'exit');

Your code fixed below:

input = prompt('Which todo would you like to delete?');
const index = todos.indexOf(input);
if (~index) {
  todos.splice(index, 1);
  console.log(`You've deleted ${input}`);
}

You can read more about Array.prototype.splice() and the bitwise operator on the corresponding MDN docs.

Follow this for the correct usage of splice.

What you would want is something like this:

  todos.splice(yourIndex,1);

First param is the start index from where you want to manipulate the array and the second is the count. Splice is in place and will modify the array.

Use indexOf method to find the index and then use splice method !

let todos = ['one' , 'two', 'three'];
let index;

// Wrap the whole code in function to make it resusable
let user_input = prompt(" Enter your to-do to delete ");
index = todos.indexOf(user_input);

if (index !== -1) {
  todos = todos.splice(index ,1);
  console.log("Entered to-do deleted successfully");
} else {
  alert("Entered to-do doesn't exists");
}

Also remember as a beginner it is good to have questions !

Related