你能解释一下这段代码吗?

时间:2015-11-28 04:37:58

标签: javascript

应该在文本中找到Eric,我明白了,但有人可以向我解释'for'循环的作用吗?感谢。

/*jshint multistr:true */

text = "Blah blah blah blah blah blah Eric \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";

var myName = "Eric";
var hits = [];

for(var i = 0; i < text.length; i++) {
    if (text[i] === "E") {
        for(var j = i; j < (myName.length + i); j++) {
            hits.push(text[j]);
        }
    }
}

if (hits.length === 0) {
    console.log("Your name wasn't found!");
} else {
    console.log(hits);
}

3 个答案:

答案 0 :(得分:2)

高级别此代码会检查每个字符,以查找字符串&#39; Eric&#39;。特别是它正在寻找角色&#39; E&#39;然后是以下3个字符。如果它找到了角色&#39; E&#39;它会将它和以下3个字符添加到输出数组

// This is defining a test string
text = "Blah blah blah blah blah blah Eric \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";

// A substring to search for
var myName = "Eric";

// An output array where the target string is stored once it is found
var hits = [];

// Look at every character inside the test string
for(var i = 0; i < text.length; i++) {
    // If the character is 'E'
    if (text[i] === "E") {
        // Add the character E and the following characters up to the length of the target string to the output
        for(var j = i; j < (myName.length + i); j++) {
            hits.push(text[j]);
        }
    }
}

// If the output has elements than we have found our target string
if (hits.length === 0) {
    console.log("Your name wasn't found!");
} else {
    console.log(hits);
}

答案 1 :(得分:2)

你的解释:

/* The \ here is used for the text going to the next line to be the same line. */

text = "Blah blah blah blah blah blah Eric \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";

/* Variable Declaration of to be found. */

var myName = "Eric";
var hits = [];

/* Going through each and every character. */

for(var i = 0; i < text.length; i++) {
    /* Checking if the current character is E */
    if (text[i] === "E") {
        /* If so, start from there, and check the next character in my name, "r". */
        for(var j = i; j < (myName.length + i); j++) {
            hits.push(text[j]);
        }
    }
}

/* If this is empty, we didn't have your name in the text. */
if (hits.length === 0) {
    console.log("Your name wasn't found!");
} else {
    console.log(hits);
}

它只是检查呃我不知道为什么如果找到Er,它将返回true。哇。哦等等,第二次检查时,ij都会递增。

答案 2 :(得分:1)

  1. for语句遍历text字符串(在每个字母上):

    for(var i = 0; i < text.length; i++)
    
  2. 如果找到一封信&#39; E&#39;:

    if (text[i] === "E")
    
  3. 运行一个myNamefor(var j = i; j < (myName.length + i); j++) 的新循环来收集&#39; E&#39;之后的下一个字母:

    hints
  4. 将每个字母推入hits.push(text[j]); 数组:

    if (hits.length === 0) {
        console.log("Your name wasn't found!");
    } else {
        console.log(hits);
    }
    
  5. 最后检查是否找到了要在控制台上显示的名称:

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.InjectMocks;
    import org.mockito.Mock;
    import org.mockito.runners.MockitoJUnitRunner;
    
    @RunWith( MockitoJUnitRunner.class )
    public class c
    {
    
        @InjectMocks
        A a;
    
        @Mock
        B b;
    
        @Test
        public void testMethodExample()
        {
            a.methodExample();
        }
    }