当传递innerHTML和(this.id)时,循环不会终止

Loop Will not terminate when passed innerHTML and (this.id)

本文关键字:循环 终止 id innerHTML this      更新时间:2023-09-26

我正在编写一个函数,该函数应该用给定参数的某个值替换某个位置的某个字符。然而,当我试图调用另一个函数内部的函数时。这是一项家庭作业,我几乎肯定我把循环写对了,但感觉自己好像在看什么(this.id).charAt((this.id).length-1)应该获取元素id的最后一个字母,并将其传递给函数。this.innerHTML旨在获取HTML的内容并将其传递到updateBoardState函数中。

function updateBoardState(newMark, squareNumber)
{
    var boardState;
    var loc;
    var winners;
    winners = getWinningCombinations();
    boardState = getBoardState();
    loc = winners.indexOf(squareNumber);window.alert(loc);
    while(loc >= 0)
    {
        replaceCharacterInString(boardState, loc, newMark);
        loc = winners.indexOf(squareNumber);
    }
    setBoardState(boardState);window.alert(setBoardState(boardState));
}
function markTheSquare() // uses the this keyword to concatenate "X" to the inner HTML of the current element.
{
    this.onclick = null; // Disassociates the oncliick function with clicked()
    this.innerHTML = getXorO(); // Concatenates the result of getXorO to the current innherHTML of the element.
    updateBoardState(this.innerHTML, (this.id).charAt((this.id).length - 1));
    setMarkCount(getMarkCount() + 1) // Increment markCount by 1.

}
function replaceCharacterInString(source, where, what)
{
    return source.substring(0, where) + what + source.substring(where + 1, source.length);
}

我的教授错别字了。

function updateBoardState(newMark, squareNumber)
{
    var boardState;
    var loc;
    var winners;
    winners = getWinningCombinations();
    boardState = getBoardState();
    loc = winners.indexOf(squareNumber);window.alert(loc);
    while(loc >= 0)
    {
        boardState = replaceCharacterInString(boardState, loc, newMark);
        loc = boardState.indexOf(squareNumber);
    }
    setBoardState(boardState);window.alert(setBoardState(boardState));
}