亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

添加不需要的字符的字符串連接問題

添加不需要的字符的字符串連接問題

一只名叫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 關注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號