My JavaScript code prompts the user to input an entry, displays a list of entries, and allows for deletion. However, there is an issue when the user enters "delete" followed by an invalid number. The code asks for a correct index, but if the user continues to input incorrect values, it stops checking altogether. I need the code to continuously validate the index input, even if the user repeatedly enters wrong numbers. It should not exit until the correct index value is provided. Below is the existing code:
let action = prompt("What would you like to do");
const todo = [];
let count=0;
let tracker=0;
let char = 'x';
while (action !== 'quit' && action !== 'q'){
if (action === 'new'){
action = prompt("What would you like to add");
todo.push(action);
tracker=0;
}
else if (action === 'list'){
console.log(char.repeat(10));
for (let elements of todo){
console.log(`${tracker}: ${elements}`);
tracker++;
}
console.log(char.repeat(10));
action = prompt("What would you like to do");
}
else if (action === 'delete'){
let index = parseInt(prompt("Enter the index of the todo you would like to delete"));
if (index<todo.length && index >-1 && index!==null){
todo.splice(index,1);
tracker=0;
}
else{
let index = parseInt(prompt("Enter a correct index"));
}
action = prompt("What would you like to do");
}
else
{
action = prompt("What would you like to do");
}
}
console.log("Ok quit the app")