如何从待办事项列表中删除特定项目?

时间:2019-09-25 18:41:19

标签: javascript arrays

有人可以解释我如何从待办事项清单中删除钓鱼吗?谢谢

let TodoList = [];
const name = [];

function getReply(command) {
  const splitString = command.split(' ');
  if (command.includes('Hello') && name.length === 0) {
    //console.log(splitString);
    const grabName = splitString.slice(-1);
    name.push(grabName);
    //console.log(name);
    return 'Nice to meet you' + ' ' + grabName;
  } else if (command.includes('Hello') && name.length !== 0) {
    return 'repeted twice';
  } else if (command.includes('What is my name')) {
    return 'your name is ' + name[0];
  } else if (command.includes("my todo")) { // Add fishing to my todo and Add singing in the shower to my todo
    const task = splitString.slice(1, -3).join(' ');
    TodoList.push(task)
    return task + " added to todo list";
  }
  if (command === 'What is on todo?') {
    return TodoList;
  }
}

console.log(getReply('Hello my name is Axel')); // Nice to meet you Axel
console.log(getReply('Hello my name is Axel, Hello my name is Axel')); //Hey Axel You repeated your name twice 
console.log(getReply("Add fishing to my todo"));
console.log(getReply("Add singing in the shower to my todo"));
console.log(getReply("What is on todo?"));
console.log(getReply("**Remove fishing from my todo**"));

1 个答案:

答案 0 :(得分:0)

您的else if (command.includes("my todo"))支票还不够,因为它与“在我的待办事项中添加钓鱼”和“从我的待办事项中删除钓鱼”相匹配。替换为else if (command.includes("Add") && command.includes("my todo"))

现在,您可以使用splice添加以下新案例。确保待办事项确实在列表中。

else if (command.includes("Remove") && command.includes("my todo")) {
  const task = splitString.slice(1, -3).join(" ");

  if(TodoList.includes("task")){
    TodoList.splice(TodoList.indexOf(task), 1);

    return task + " removed from todo list";
  }

  return task + " is not on the todo list";
}

完整代码:

let TodoList = [];
const name = [];

function getReply(command) {
  const splitString = command.split(" ");

  if (command.includes("Hello") && name.length === 0) {
    //console.log(splitString);
    const grabName = splitString.slice(-1);

    name.push(grabName);

    //console.log(name);
    return "Nice to meet you " + grabName;
  } else if (command.includes('Hello') && name.length !== 0) {
    return "Repeated twice";
  } else if (command.includes('What is my name')) {
    return "Your name is " + name[0];
  } else if (command.includes("Add") && command.includes("my todo")) { // Add fishing to my todo and Add singing in the shower to my todo
    const task = splitString.slice(1, -3).join(" ");

    TodoList.push(task);

    return task + " added to todo list";
  } else if (command.includes("Remove") && command.includes("my todo")) {
    const task = splitString.slice(1, -3).join(" ");
  
    if(TodoList.includes("task")){
      TodoList.splice(TodoList.indexOf(task), 1);
      
      return task + " removed from todo list";
    }
    
    return task + " is not on the todo list";
  }
  if (command === "What is on todo?") {
    return TodoList;
  }
}

console.log(getReply("Hello my name is Axel")); // Nice to meet you Axel
console.log(getReply("Hello my name is Axel, Hello my name is Axel")); // Hey Axel You repeated your name twice
console.log(getReply("Add fishing to my todo"));
console.log(getReply("Add singing in the shower to my todo"));
console.log(getReply("What is on todo?"));
console.log(getReply("Remove fishing from my todo"));
console.log(getReply("What is on todo?"));