簡體   English   中英

允許用戶編輯數組

[英]allow the user to edit an array

這是我的問題。 我試圖創建一個 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>";
     } "";}

好吧,我在您的代碼中看到了很多問題,實際上很多人都可以給出明確的答案。 對於初學者,您應該以可以區分它們的 function 的方式命名變量。 如果一切都被稱為assignsomething,那么您很快就會忘記它們。 不要使用while循環來提示,因為當它打開時您無權訪問它。 只需在執行后選擇該值,然后對其進行處理。

但是,這可能是一個很好的練習,可以以非常老式的方式進行提示,但您希望很快切換到基於 JS 的組件庫(即 Bootstrap)或適當的 JS 框架(帶有 vuetify 或 react 的 vuejs)。 提示根本無法設置樣式,用戶想要提供真實 UX 的適當 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');
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM