简体   繁体   English

尝试添加和删除数组中的项目

[英]Trying to add and remove items from an array

The script works by asking user for add or remove an item in the array. 该脚本通过要求用户添加或删除数组中的项目来工作。 Then asks to continue this loop. 然后要求继续此循环。 The problem here is that my script doesn't seem to match my user's input (removeItem) to the item in the list (myList[i]). 这里的问题是我的脚本似乎与用户输入(removeItem)与列表(myList [i])中的项目不匹配。 I'm at a lost as to why this is failing to match. 我迷茫的为什么这不能匹配。

// new method for removing specific items from a list
Array.prototype.remove = function(from,to) {
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};

printList = function() {
    var listLength = myList.length;
    for (i = 0; i < listLength; i++) {
        document.write(i + ":");
        document.write(myList[i] + "<br/>");
    };
    document.write("<br/><br/>");
};

// initial list
var myList = new Array ();
if (myList.length === 0) {
    document.write("I have " + myList.length + " item in my list. It is: <br/>");
}
else {
    document.write("I have " + myList.length + " items in my list. They are: <br/>");
}
printList();

var continueAdding = "yes";
var askToContinue = "";

while (continueAdding === "yes") {
    // loop
    var askUser = prompt("What do you want to [A]dd or [R]emove an item to your inventory?").toUpperCase();
    switch (askUser) {
        case "A": { // add an user specified item to the list
            var addItem = prompt("Add something to the list");
            myList.push(addItem);
            printList();
            break;
        }
        case "R": { // remove an user specified item from the list
            var removeItem = prompt("what do you want to remove?"); 
            var listLength = myList.length;
            for (i = 0; i < listLength; i++) {
                if (removeItem === myList[i]) {
                    document.write("I found your " + removeItem + " and removed it.<br/>");
                    myList.remove(i);
                }
                else {
                    document.write(removeItem + " does not exist in this list.<br/>");
                    break;
                }
                if (myList.length === 0) {
                    myList[0] = "Nada";
                }
            };
            printList();
            break;
        }
        default: {
            document.write("That is not a proper choice.");
        }
    };

    askToContinue = prompt("Do you wish to continue? [Y]es or [N]o?").toUpperCase(); // ask to continue
    if (askToContinue === "Y") {
        continueAdding = "yes";
    }
    else {
        continueAdding = "no";
    }
}

Your loop never allows it to loop through all the items, because it breaks on the first iteration if the item doesn't match. 您的循环永远不允许它循环遍历所有项目,因为如果项目不匹配,它将在第一次迭代时中断。

The break statement should be in the if block, not in the else block - use this instead: break语句应位于if块中,而不应位于else块中-请改用以下语句:

for (i = 0; i < listLength; i++) {
    if (removeItem === myList[i]) {
        document.write("I found your " + removeItem + " and removed it.<br/>");
        myList.remove(i);
        break;
    }
    else {
        document.write(removeItem + " does not exist in this list.<br/>");
    }
};

if (myList.length === 0) {
    myList[0] = "Nada";
}

Also, note that it's looking for an exact match, case sensitive, same punctuation, and everything. 另外,请注意,它正在寻找完全匹配,区分大小写,相同标点符号以及所有内容。 If you want it to be a little more lenient you'll need to modify the script to convert both strings to lowercase and strip punctuation before comparing them. 如果您希望它宽松一些,则需要在将它们比较之前,修改脚本以将两个字符串都转换为小写并去除标点符号。

Edit: Just noticed something else -- testing for an empty list needs to be done outside the loop. 编辑:刚注意到其他东西-测试空列表需要在循环外完成。 I updated the above code to reflect this. 我更新了上面的代码以反映这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM