1

我想通了,谢谢。我需要将正文移动到 html。更改了正文部分的一些标签。

            }

            else
            {
                window.alert ("You entered an invalid character (" + enterLetter + ") please re-enter");
                secondPrompt();
            }

        }

</script>

<body onload = "firstPrompt();">

    <h2>
        Word Checker
    </h2>

    </body>
    </html>
4

2 回答 2

2

每次找到匹配项时,您都可以增加 indexOf -

function indexFind(string, charac){
    var i= 0, found= [];
    while((i= string.indexOf(charac, i))!= -1) found.push(i++);
    return found;
}

indexFind('今天比以前更像','o');

/* 返回值:(数组)6,22,48 */

于 2012-10-28T21:46:02.093 回答
0

indexOf递归使用:

function findMatches(str, char) {
    var i = 0,
        ret = [];
    while ((i = str.indexOf(char, i)) !== -1) {
        ret.push(i);
        i += char.length; //can use i++ too if char is always 1 character
    };
    return ret;
}

代码中的用法:

var matches = findMatches(enterWord, enterLetter);
if (!matches.length) { //no matches
    document.write ("String '" + enterWord + "' does not contain the letter '" + enterLetter + ".<br />");
} else {
    for (var i = 0; i < matches.length; i++) {
        document.write ("String '" + enterWord + "' contains the letter '" + enterLetter + "' at position " + matches[i] + ".<br />");
    }
}

现场演示

完整源代码(对您的上一个问题进行了一些调整)

于 2012-10-28T21:46:06.023 回答