首页 > 解决方案 > 在 JS 中调试 ToDoList 类型的任务

问题描述

我正在尝试制作一个在控制台中运行并带有提示的 todolist 类型的任务。

它的想法如下: 提示显示,您可以键入以下操作之一:新项目/删除项目/列出项目/退出。

问题是,例如,当我键入“new”然后“return”(完成列表后)然后对于 ex,我键入“list”(控制台显示列表数组),但是当我想再次键入“new”(添加adional items) ,任务关闭。

我看不出我在代码中是否错了......为什么当我输入“new”时“new”while循环没有第二次启动?

这是我目前拥有的代码:

let input = prompt("Welcome to TO-DO-LIST! Please chose an action : 1. Add item - type 'new' / 2. List all todos - type 'list' / 3. Remove a specific todo - type 'delete' / 4. Quit - type 'quit'")

const list = [];

// NEW ITEMS FUNCTION
while (input === 'new'){
  let item = prompt("What would you like to do? Type 'return' to go back to main menu");
  if(item === 'return'){
    input = prompt("Welcome to TO-DO-LIST! Please chose an action : 1. Add item - type 'new' / 2. List all todos - type 'list' / 3. Remove a specific todo - type 'delete' / 4. Quit - type 'quit'")
  }else{
    list.push(item);
  }
}

// REMOVE ITEMS FUNCTION
while(input ==='delete'){
  let removeItem = prompt("What would you like to remove? Type 'return' to go back to main menu")
  if(removeItem === 'return'){
    input = prompt("Welcome to TO-DO-LIST! Please chose an action : 1. Add item - type 'new' / 2. List all todos - type 'list' / 3. Remove a specific todo - type 'delete' / 4. Quit - type 'quit'")
  }else{
    list.splice(0, 1, removeItem);
  }
}

// QUIT FUNCTION
if(input==='quit'){
  console.log("You have exited the list");
}

// SHOW LIST FUNCTION
while(input ==='list'){
  console.log('---------');
  for(let i = 0; i < list.length; i++){
    console.log(`${i}: ${list[i]}`);
  }
  console.log('---------');
  input = prompt("Welcome to TO-DO-LIST! Please chose an action : 1. Add item - type 'new' / 2. List all todos - type 'list' / 3. Remove a specific todo - type 'delete' / 4. Quit - type 'quit'")
}

标签: javascriptloopswhile-loop

解决方案


推荐阅读