Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

52
Vistas
Debugging a ToDoList type task in JS

A am trying to make a todolist type task that runs in the console with prompts.

It is thought like following: Prompt shows and you can type one of the following actions: new item / delete item / list items / quit.

Problem is that for example when i type "new" then "return" (after finishing the list) then for ex i type "list" (console displays the list array), but when i want again to type "new" (to add adional items) , the task closes.

I can't see were i am wrong in the code... Why doesn't the "new" while loop launch the 2nd time when i type "new" ?

Here is the code i have for the moment:

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'")
}
7 months ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos