我正在尝试使用 codecademy 的说明构建一个刽子手游戏,但我很难独自跟随。在下面的guessLetter 函数中,我按照他们给我的说明添加了两行代码
这
shown = alterAt(checkLetter,letter,shown); ///this is mine
还有这个
checkLetter = indexOf(letter, checkLetter +1);
但是,我无法通过测试。如果您能提供帮助,我将不胜感激。
// Skip to guessLetter(). You shouldn't need to touch this function.
function alterAt ( n, c, originalString ) {
return originalString.substr(0,n) + c + originalString.substr(n+1,originalString.length);
}
function guessLetter( letter, shown, answer ) {
var checkLetter = -1; // This variable will hold the indexOf()
checkLetter = answer.indexOf(letter); // Single Argument Version starting at 0
while ( checkLetter >= 0 ) {
// Replace the letter in shown with alterAt() and then store in shown.
shown = alterAt(checkLetter,letter,shown); ///this is mine
// Use indexOf() again and store in checkLetter
checkLetter = indexOf(letter, checkLetter +1); ///this is mine
}
// Return our string, modified or not
return shown;
}
更新
如果猜测是正确的,它应该返回被猜测修改的显示。例如,如果单词是 tree 而玩家猜到 'e',它应该返回 '__ee'。这是我无法使用“随便”这个词来工作的小提琴:http: //jsfiddle.net/mjmitche/YAPWm/2/