我在下面开发了这个算法。目标是在第一个字母是元音的情况下返回它。示例:'egg' -> 它应该返回:'e' 并且当我有一个辅音时它应该返回,就像这个例子:'car' -> 它应该返回 'c'。当我有一组像“手套”这样的辅音时,它必须返回“gl”。只有在单个元音的情况下,它才按预期成功返回。在单个辅音或辅音簇的情况下,返回时会添加一个不受欢迎的元音,如下例所示:
solution('egg') // -> It is returning 'e' as expected. OK RIGHT!
solution('car') // -> It is returning 'ca'. It is expected: 'c'. WRONG!
solution('glove') // -> It is returning 'glo'. It is expected: 'gl'. WRONG!
有谁知道我做错了什么?谢谢
function solution(str) {
let vowels = /[aeiou]/gi
let currentIndex = 0
let currentCharacter = str[currentIndex ]
let consonants = ''
let outputStr = ''
if (vowels.test(currentCharacter)) {
outputStr = currentCharacter
} else {
while (true) {
if (!vowels.test(currentCharacter)) {
currentCharacter = str[currentIndex]
consonants += currentCharacter
currentIndex ++
} else {
break
}
}
outputStr = `${consonants}`
}
return outputStr
}
console.log(solution('glove'))
尚方宝剑之说
慕码人2483693
相关分类