创建一个函数,接受一个单词,如果该单词有两个连续的相同字母,则 returns 为 true

Create a function that takes a word and returns true if the word has two consecutive identical letters

// 创建一个接受单词的函数,如果单词有两个连续的相同字母,则 returns 为真。

我做错了什么?

module.exports = (word) => {
    for (let i = 0; i <= word.length; i++) {
        for (let j = i + 1; j <= word.length; j++) {
            if (word[j] == word[i]) {
                return true;
            }
        } return false;
        
    } 

};

您只需循环 1 次即可完成此操作。

function hasConsecutiveIdenticalLetters(word){
    for (let i = 1; i < word.length; i++) {
        if (word[i-1] === word[i]) {
            return true;
        }
    }
    return false;
}

您也可以使用 some

实现如下所示
const hasConsecutiveIdenticalLetters = (word: string) => (word).split('').some((letter, index) => letter === word[index + 1]);