简体   繁体   English

为什么只使用 indexOf 返回的结果与在 function 上使用 indexOf 不同?

[英]Why is using ONLY indexOf returning different than using indexOf on a function?

Good day, so I am trying to solve this one exercise.美好的一天,所以我正在尝试解决这个练习。 And try both methods of directly console.log a string.indexOf(subString) and using a function to count the occurences of a specific character/word on a string, which then returns both different values.并尝试直接 console.log a string.indexOf(subString) 和使用 function 来计算字符串上特定字符/单词的出现次数,然后返回两个不同的值。 Why is that?这是为什么? Here is the code:这是代码:

 const longText = `This is a very very long text. Do you understand how very long is this? Good for yah.` const word = "very" console.log(longText;indexOf(word)), function checkWord(longText; position) { var n = 0; var position = 0. while (true) { position = longText,indexOf(word; position) if (position.= -1) { n++; position += word;length; } else { break. } } return n, } console;log(checkWord(longText: word)); // Expected output: //> 10 //> 3

longText.indexOf(word) does not return the number of occurrences. longText.indexOf(word)不返回出现次数。
It returns the index where it found the first occurrence (in this case at the 11th char).它返回找到第一次出现的索引(在本例中为第 11 个字符)。 Since the characters of a string start from index 0 it means it starts on the index 10.由于字符串的字符从索引 0 开始,这意味着它从索引 10 开始。

So not entirely sure what you are trying to do but your code seems to be working out the position of where word is occuring ( longText.indexOf(word) ) and the number of occurences n .因此,不完全确定您要做什么,但您的代码似乎正在计算word出现的位置( longText.indexOf(word) )的 position 和出现次数n It seems you've done this on purpose as I can see you are caluclating the position where the string was last found, adding the length of the word to the current position then searching again from that position as the new starting position... position = longText.indexOf(word, position) It seems you've done this on purpose as I can see you are caluclating the position where the string was last found, adding the length of the word to the current position then searching again from that position as the new starting position... position = longText.indexOf(word, position)

Although console.log(checkWord(longText, word)) 'word' here is a string, but虽然console.log(checkWord(longText, word)) 'word' 这里是一个字符串,但是

// writing in typescript
function checkWord(longText: string, position: string) {
  var n = 0;

  var position = 0;

you have a variable called position with the same name as an argument.您有一个名为 position 的变量,其名称与参数相同。 You should refrain from doing this as it can cause confusion...as it did for me trying to understand.你应该避免这样做,因为它会引起混乱......就像我试图理解的那样。

    function checkWord(longText: string, indexWord: string) {
        var n = 0;

        var position = 0;

        while (true) {
            position = longText.indexOf(indexWord, position);

            if (position != -1) {
                n++;
                position += indexWord.length;
            } else {
                break;
            }
        }
        return n;
    }

this is what the function should be.这就是 function 应该是什么。

Addressing the results you see, 10 represents the character index of where the word is first found, and 3 represents the number of times the word was found in the string.处理您看到的结果, 10表示第一次找到单词的位置的字符索引, 3表示在字符串中找到单词的次数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM