allow the user to edit an array

Viewed 26

here is my issue. im trying to make a for loop that lets me show the assignments and then gives a user the option to edit, delete, and even add to the array....but no matter what i do it doesnt make sense or it wont work.

var remove1="Delete";

let assignments = new Array ("chapter1", "chapter2", "chapter3", "chapter4", "chapter5", "chapter6", "chapter7", 
"chapter8", "chapter9", "chapter10", "chapter11", "chapter12", "chapter13", "chapter14", "chapter15");
while (assignin != "") {
     {var assignin = window.prompt("So what would you like to do?","");}

     if ((assignin===null)||(assignin===""))
     {document.write("sorry not an available option restart and try again");}

     else if (assignin === remove1)
     
     {var remove2 = window.prompt,text = "ok which would you like to remove<br>"
     for (let i = 0; i < assignment.length; i++) {
       text += assignment[i] + "<br>";
     } "";}
1 Answers

Well there are many issues I see in your code actually to many to give a clear answer. For starters you should name your variables in a way that you can distinguish their function. If everything is called assignsomething than you loose track of them very fast. Don't use while loop for prompts as you don't have access to it when it's open. Just pick the value after it's executed and do something then with it.

BUT This might be a great exercise to do prompts in the very oldschool way but you want very soon to switch over to a JS-Based-Component-Library (i.e. Bootstrap) or a proper JS-Framework (vuejs with vuetify or react). Prompts can't be styled at all and users want proper UI that provide real UX.

Working code to get you going (without the business logic):

const DELETE_ITEM = 'delete';
const ADD_ITEM = 'add';
const commandList = [ADD_ITEM, DELETE_ITEM]
let assignments = ['chapter1', 'chapter2', 'chapter3', 'chapter4'];
let command = window.prompt('So what would you like to do? `Add` or `Delete`','')

if (commandList.includes(command.toLocaleLowerCase())){
  switch(command.toLocaleLowerCase()) {
    case ADD_ITEM : 
      console.log('add item')
      let addPrompt = window.prompt('type in what you want to add')
      if (addPrompt.trim() && addPrompt.trim().length) {
        console.log('Item to add is:', addPrompt)
      } else {
        console.warn('Sorry this value isn\'t allowed')
      }
      break
    case DELETE_ITEM : 
      let removePrompt = window.prompt('ok which would you like to remove')
      if (assignments.includes(removePrompt)) {
        console.log('Item to remove is:', removePrompt)
      } else {
        console.warn('Sorry this value doesen\'t exist')
      }
      break
  }
} else {
  console.log('sorry not an available option restart and try again');
}
Related