Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 解释从word中获取字母的函数_Javascript_Function_While Loop_Substr - Fatal编程技术网

Javascript 解释从word中获取字母的函数

Javascript 解释从word中获取字母的函数,javascript,function,while-loop,substr,Javascript,Function,While Loop,Substr,我正在用javascript制作一个刽子手游戏,我很难理解这个函数中的代码 以下是函数: function getLetter(word,letter,display){ // This method is called by the Hangman program when your isLetterInWord function // above returns true. // The parameters passed in are the guessed let

我正在用javascript制作一个刽子手游戏,我很难理解这个函数中的代码

以下是函数:

function getLetter(word,letter,display){
    // This method is called by the Hangman program when your isLetterInWord function
    // above returns true.
    // The parameters passed in are the guessed letter, the secret word,
    // and the current display state of the secret word.
    // This method will return a new display state of the secret word based on the matching letter.
    // REPLACE THIS CODE WITH YOUR getLetter() METHOD
    while (word.search(letter) != -1) {
        var index=word.search(letter)
        display = display.substr(0, index) + letter + display.substr(index + 1);        
        word = word.substr(0, index) + '-' + word.substr(index + 1);
    }
        return display;
}
我不太明白的部分是:

display = display.substr(0, index) + letter + display.substr(index + 1);        
word = word.substr(0, index) + '-' + word.substr(index + 1);
基本上,这个程序需要一个单词,找到字母的数量,并用连字符替换它们。 例如,单词“boat”将变成“----”

上述函数的作用是用相应的连字符替换字母guess(正确)

这是整个项目的背景代码

// Hangman Project
//RETURN A 'HIDDEN' VERSION OF THE SUPPLIED SECRET WORD
function getDisplay(word)
{   
    // Given a string, "word", return a hidden version of it consisting
    // of dashes for the display.
    // REPLACE THIS CODE WITH YOUR getDisplay() METHOD

    var disp="";
    for (var i=0; i < word.length; i++ ){
        disp = disp +'-';
    }
    return disp;
}

//FIND IF THE LETTER IS IN THE WORD
function isLetterInWord(word,letter){
    // Given the word "word", check if it contains the letter "letter".
    // REPLACE THIS CODE WITH YOUR isLetterInWord() METHOD

    if(word.search(letter) != -1) {
        return true;
    } else {
        return false;
    }

}
//刽子手计划
//返回所提供密码的“隐藏”版本
函数getDisplay(word)
{   
//给定一个字符串“word”,返回它的隐藏版本
//显示的破折号数。
//用getDisplay()方法替换此代码
var disp=“”;
for(变量i=0;i

任何解释这两条车道的帮助都将不胜感激。谢谢。

假设单词是
animal
,玩家已经猜对了
a
l
。输入将是:

word = "animal"
letter = "i"
display = "a---al"
请遵循以下代码:

var index = word.search(letter);
现在
index=2
(计数以0为基础)

word.substr(0,2)
“a-”
<代码>单词.substr(3)
“-al”
。因此,当所有内容都连接在一起时,
word=“a-i-al”

索引+1
跳过被替换的字符

下一行

word = word.substr(0, index) + '-' + word.substr(index + 1);

与此类似,将找到的字母替换为
-
。如果字母在单词中多次出现(如
a
animal
中出现),则需要这样做,以便
while
循环不会一直尝试替换相同的位置。

MDN可能会有所帮助,理解和将帮助你理解整个代码。你怎么会在理解你正在编写的代码时遇到困难?@dandavis显然不是他的,可能是他在网上找到的某个刽子手库。@Teemu我两个都懂。我不明白的是连字符是在什么时候被去掉的。我看到显示器如何将字母添加到连字符中,但我看不到多余的连字符何时被删除。请查看
display.substr(0,index)
display.substr(index+1)
return。特别是,与单词中的字符数相比,计算其中的字符数。
word = word.substr(0, index) + '-' + word.substr(index + 1);