允许用户编辑数组



这是我的问题。我试图制作一个for循环,让我显示分配,然后给用户编辑、删除甚至添加到数组的选项。。。。但无论我做什么都没有意义,或者根本不起作用。

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>";
} "";}

我在您的代码中看到了很多问题,实际上很多问题都需要给出明确的答案。首先,您应该以能够区分其函数的方式命名变量。如果每件事都被称为任务,那么你会很快忘记它们。不要使用while循环进行提示,因为当它打开时,您无法访问它。只需在执行后选择值,然后用它做一些事情。

但是这可能是一个很好的练习,可以用老式的方式进行提示,但您希望很快切换到基于JS的组件库(即Bootstrap(或适当的JS框架(带有vuetify或react的vuejs(。提示根本无法设置样式,用户需要提供真实用户体验的适当UI。

让你继续工作的工作代码(没有业务逻辑(:

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');
}

最新更新