添加不需要的字符的字符串连接问题

我在下面开发了这个算法。目标是在第一个字母是元音的情况下返回它。示例:'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'))


一只名叫tom的猫
浏览 119回答 2
2回答

尚方宝剑之说

您当前尝试的问题是以下代码段:while (true) {&nbsp; if (!vowels.test(currentCharacter)) {&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; currentCharacter = str[currentIndex]&nbsp; &nbsp; consonants += currentCharacter&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; currentIndex ++&nbsp;&nbsp; } else {&nbsp; &nbsp; break&nbsp; }}这里有两件事出了问题。你vowels测试currentCharacter.&nbsp;如果currentCharacter不是元音,则应将其直接添加到输出中。您当前首先更改 的值,currentCharacter然后再将其添加到输出。您当前设置了一个新值currentCharacterbefore incrementing&nbsp;currentIndex。这应该在之后完成。让我展开循环并演示问题:/* str = "car"&nbsp;* vowels = /[aeiou]/gi&nbsp;* currentIndex = 0&nbsp;* currentCharacter = "c"&nbsp;* consonants = ""&nbsp;*/if (!vowels.test(currentCharacter)) //=> truecurrentCharacter = str[currentIndex];/* currentIndex = 0&nbsp;* currentCharacter = "c"&nbsp;* consonants = ""&nbsp;*/consonants += currentCharacter/* currentIndex = 0&nbsp;* currentCharacter = "c"&nbsp;* consonants = "c"&nbsp;*/currentIndex ++/* currentIndex = 1&nbsp;* currentCharacter = "c"&nbsp;* consonants = "c"&nbsp;*/if (!vowels.test(currentCharacter)) //=> truecurrentCharacter = str[currentIndex];/* currentIndex = 1&nbsp;* currentCharacter = "a"&nbsp;* consonants = "c"&nbsp;*/consonants += currentCharacter/* currentIndex = 1&nbsp;* currentCharacter = "a"&nbsp;* consonants = "ca"&nbsp;*/currentIndex ++/* currentIndex = 2&nbsp;* currentCharacter = "a"&nbsp;* consonants = "ca"&nbsp;*/if (!vowels.test(currentCharacter)) //=> false要解决此问题,您只需移动 的赋值currentCharacter并将其放在 的增量之后currentIndex。while (true) {&nbsp; if (!vowels.test(currentCharacter)) {&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; consonants += currentCharacter&nbsp; &nbsp; currentIndex ++&nbsp;&nbsp; &nbsp; currentCharacter = str[currentIndex] // <- moved two lines down&nbsp; } else {&nbsp; &nbsp; break&nbsp; }}&nbsp;function solution(str) {&nbsp;&nbsp;&nbsp; let vowels = /[aeiou]/gi&nbsp; let currentIndex = 0&nbsp; let currentCharacter = str[currentIndex ]&nbsp;&nbsp; let consonants = ''&nbsp; let outputStr = ''&nbsp; if (vowels.test(currentCharacter)) {&nbsp; &nbsp; outputStr = currentCharacter&nbsp; &nbsp;&nbsp; } else {&nbsp; &nbsp; while (true) {&nbsp; &nbsp; &nbsp; if (!vowels.test(currentCharacter)) {&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; consonants += currentCharacter&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; currentIndex ++&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; currentCharacter = str[currentIndex]&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; outputStr = `${consonants}`&nbsp; &nbsp;&nbsp; }&nbsp; return outputStr}console.log(solution('glove'))

慕码人2483693

您可以使用以锚点开头的交替^来匹配断言[aeiou]右侧辅音的元音,或者匹配 1 个或多个辅音的其他方式。\b(?:[aeiou](?=[b-df-hj-np-tv-z])|[b-df-hj-np-tv-z]+(?=[aeiou]))正则表达式演示function solution(str) {&nbsp; const regex = /^(?:[aeiou](?=[b-df-hj-np-tv-z])|[b-df-hj-np-tv-z]+(?=[aeiou]))/i;&nbsp; let m = str.match(regex);&nbsp; return m ? m[0] : str;}console.log(solution('egg'));console.log(solution('car'));console.log(solution('glove'));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript