为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

一只名叫tom的猫 2023-05-19 15:05:28
我在下面开发了这个算法。目标是在第一个字母是元音的情况下返回它。示例:'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'))
查看完整描述

2 回答

?
尚方宝剑之说

TA贡献1788条经验 获得超4个赞

您当前尝试的问题是以下代码段:


while (true) {

  if (!vowels.test(currentCharacter)) {     

    currentCharacter = str[currentIndex]

    consonants += currentCharacter    

    currentIndex ++ 

  } else {

    break

  }

}

这里有两件事出了问题。

  1. vowels测试currentCharacter. 如果currentCharacter不是元音,则应将其直接添加到输出中。您当前首先更改 的值,currentCharacter然后再将其添加到输出。

  2. 您当前设置了一个新值currentCharacterbefore incrementing currentIndex。这应该在之后完成。

让我展开循环并演示问题:

/* str = "car"

 * vowels = /[aeiou]/gi

 * currentIndex = 0

 * currentCharacter = "c"

 * consonants = ""

 */


if (!vowels.test(currentCharacter)) //=> true


currentCharacter = str[currentIndex];

/* currentIndex = 0

 * currentCharacter = "c"

 * consonants = ""

 */


consonants += currentCharacter

/* currentIndex = 0

 * currentCharacter = "c"

 * consonants = "c"

 */


currentIndex ++

/* currentIndex = 1

 * currentCharacter = "c"

 * consonants = "c"

 */


if (!vowels.test(currentCharacter)) //=> true


currentCharacter = str[currentIndex];

/* currentIndex = 1

 * currentCharacter = "a"

 * consonants = "c"

 */


consonants += currentCharacter

/* currentIndex = 1

 * currentCharacter = "a"

 * consonants = "ca"

 */


currentIndex ++

/* currentIndex = 2

 * currentCharacter = "a"

 * consonants = "ca"

 */


if (!vowels.test(currentCharacter)) //=> false

要解决此问题,您只需移动 的赋值currentCharacter并将其放在 的增量之后currentIndex。


while (true) {

  if (!vowels.test(currentCharacter)) {     

    consonants += currentCharacter

    currentIndex ++ 

    currentCharacter = str[currentIndex] // <- moved two lines down

  } else {

    break

  }

}

 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)) {     

        consonants += currentCharacter    

        currentIndex ++ 

        currentCharacter = str[currentIndex]

      } else {

        break

      }

    }


    outputStr = `${consonants}`   

  }


  return outputStr

}


console.log(solution('glove'))


查看完整回答
反对 回复 2023-05-19
?
慕码人2483693

TA贡献1860条经验 获得超9个赞

您可以使用以锚点开头的交替^来匹配断言[aeiou]右侧辅音的元音,或者匹配 1 个或多个辅音的其他方式。

\b(?:[aeiou](?=[b-df-hj-np-tv-z])|[b-df-hj-np-tv-z]+(?=[aeiou]))

正则表达式演示

function solution(str) {

  const regex = /^(?:[aeiou](?=[b-df-hj-np-tv-z])|[b-df-hj-np-tv-z]+(?=[aeiou]))/i;

  let m = str.match(regex);

  return m ? m[0] : str;

}


console.log(solution('egg'));

console.log(solution('car'));

console.log(solution('glove'));


查看完整回答
反对 回复 2023-05-19
  • 2 回答
  • 0 关注
  • 118 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信