1

私はそれを理解しました、ありがとう。本文をhtmlに移動する必要があります。body セクションのいくつかのタグを変更しました。

            }

            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 に答える