这段代码应该返回hits = [“” Heather,“ Heather”,“ Heather”],但不是。我不太确定自己在做什么错。

var text = ["Heather", "kfdjsalfjdai", "fdjafhjdksafh", "Heather", "Heather", "fjdiafjdoisfhoids"];
var myName = "Heather";
var hits = [];
for(var i = 0; i < text.length; i++) {
    if (text[i] === myName[i]) {
        for(var j = i; j < (myName.length + i); j++); {
            hits.push(text[j]);
        }
    }
}

最佳答案

检查myName变量。

var text = ["Heather", "kfdjsalfjdai", "fdjafhjdksafh", "Heather", "Heather",
                                        "fsomething"];
var myName = "Heather";
var hits = [];

for(var i = 0; i < text.length; i++) {
    if (text[i] === myName) {
            hits.push(text[i]);
    }
}
console.log(hits);


Here是工作示例

关于javascript - JavaScript-使用数组检查字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34160635/

10-09 18:49