我正在编写一个函数,该函数应该使用给定参数替换某个特定位置的特定字符。但是,当我尝试在另一个内部调用该函数时。这是一个家庭作业,我几乎肯定我已经正确地写了循环,但感觉好像我在寻找一些东西。 (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);
}
答案 0 :(得分:0)
我的教授打错了。
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));
}