1

Within my code, I have run into a bug.

If you run this javascript code and look at what the console outputs.

I will have a todo list I will enter new put in something I want to do great its added to the array. I will then hit new again, great, added another thing to do to the array.

Right now I should have two items in my array [0] and [1] correct?

If I try to delete the items it does not work. I have to use the delete key terms like two times then hit the list for it to work again? Idk if its just a bug on my side but run my javascript application use the keyterms in the HTML page and try and add new things to do, list them, then try deleting them. It works but it doesn't work on the first try.

Warning will cause the browser to continuously open alerts until quit is typed

var todos = [""];
var input = prompt("What would you like to do today?");

while (input !== "quit") {
  if (input === "list") {
    todos.forEach(function(todo, i) {
      console.log(i + ": " + todo);
    });
  } else if (input === "new") {
    var newTodo = prompt("Enter new thing to do");
    todos.push(newTodo);
  } else if (input === "delete") {
    var index = prompt("Enter index of todo to delete");
    todos.splice(index, 1);
  }

  input = prompt("What would you like to do?");
}
console.log("Ok, you have quit the application");
<!DOCTYPE html>
<html>

<head>
  <title>Todo List</title>

</head>

<body>
  <h1>To Do List</h1>
  <ul>
    <li>"new" - Add A Todo</li>
    <li>"list" - List all Todos</li>
    <li>"Delete" A Specific Todo</li>
    <li>"quit" - quit the application</li>


  </ul>
</body>

</html>

1 Answer 1

3

It's hard to tell what you are doing to produce negative results, but here is my best guess:

var todos = [""]; means that your array starts with an empty string at todos[0]. You should initialize the array with var todos = [];.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey man you're absolutely right! It works now! When I had my array with "" inside of it it started the index at 0 already. I kept asking myself why am I aloud to delete the first item with an index of 1 but not 0 haha. Thanks man!!
Spot on! That's the first thing I also noticed. He should initialize an empty array, either as var todos = []; (preferred) or alternatively as var todos = new Array();.
I will accept this answer when it lets me btw in 3 minutes :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.