【问题标题】:just change the vowel to the next letter in javascript只需将元音更改为javascript中的下一个字母
【发布时间】:2019-08-22 09:37:52
【问题描述】:

首先我要证明句子中是否有元音,如果有元音则用下一个字母代替。不是元音的字母不会被替换,但在输出中保持不变。

我做的编码结果变得不合适了。

function changeVocals(str){
    var split = str.split('')
    var vocal = 'aiueo'
    var change = 'bjvfp'
    var a = ''

    for (var i = 0; i < split.length; i++) {
        for (var j = 0; j < vocal.length; j++) {
            if (vocal[j] === split[i]) {
                a = a + change
            } else {
                a = a + split[i]
            }
        }
    }
    return a     
}

console.log(changeVocals('Alexa')); //'Blfxb'

我希望 'Alexa' 的输出是 'Blfxb',但实际输出是句子似乎是重复的。

实际输出:AAAAAllllleeebjvfpexxxxxbjvfpaaaa

【问题讨论】:

    标签: javascript loops if-statement conditional-statements


    【解决方案1】:

    您可以使用对象来映射值,并根据大小写替换匹配的值

    const mapper = {
      a: 'b', e: 'f', i: 'j', o: 'p', u: 'v',
      A: 'B', E: 'F', I: 'J', O: 'P', U: 'V'
    }
    
    const changeVocals = (string) => {
      return string.replace(/[aeiou]/gi, match => mapper[match])
    }
    console.log(changeVocals('Alexa'));

    【讨论】:

    • 这是解决这个问题的一种聪明而简洁的方法。 +1。
    【解决方案2】:

    可以用map完成;

    var vowels = ['a', 'e', 'i', 'o', 'u'];
    var text = 'Alexa';
    var result = text.split('').map(x => vowels.indexOf(x.toLowerCase())>=0 ? String.fromCharCode(x.charCodeAt()+1) : x).join('');
    
    console.log(result);

    【讨论】:

      【解决方案3】:

      function changeVocals(str){ 
      return [...str].map(letter => "aeiouAEIOU".contains(letter) ? String.fromCharCode(letter.charCodeAt(0)+1) : letter).join('');
      }
      
      changeVocals("Alexa");

      但不知何故,代码 sn-p 无法识别 contains 函数

      【讨论】:

      • 很高兴为您提供帮助,如果答案正确,请标记为正确:)
      【解决方案4】:

      我在您的尝试中发现了几个错误。我试图修复它以使其正常工作,如下所示。但是您的尝试非常接近。我希望我的 cmets 能帮助你理解你犯错的那两个地方。

      function changeVocals(str) {
          var split = str.split('');
          var vocal = 'aiueo';
          var change = 'bjvfp';
          var a = ''
      
          for (var i = 0; i < split.length; i++) {
              for (var j = 0; j < vocal.length; j++) {
                  // When there is a match we will not continue
                  // so break out of the loop.
                  if (vocal[j] === split[i]) {
                      // this test if the match is lowercase to lowercase
                      a = a + change[j];
                      break;
                  } else if (vocal[j].toUpperCase() === split[i]) {
                      // this test if the match is uppercase to uppercase
                      a = a + change[j].toUpperCase();
                      break;
                  }
              }
              // When there is no match found we just copy the input
              // character to the resulting string.
              // We figured there is no match found when we see
              // that the resulting string length is less than the current
              // value of i
              if (a.length < i + 1) a = a + split[i];
          }
      
          return a;
      }
      
      console.log(changeVocals('Alexa'));
      //'Blfxb'
      

      【讨论】:

        【解决方案5】:

        function convertot(string) {
          const vowels = ['a', 'e', 'i', 'o', 'u'];
          let str = '';
          for (let i = 0; i < string.length; i += 1) {
            const element = string[i];
            const isPresent = vowels.findIndex((f) => { return f === element.toLowerCase(); });
            if (isPresent !== -1) {
              str += String.fromCharCode(element.charCodeAt() + 1);
            } else {
              str += element;
            }
          }
          return str;
        }
        
        const text = 'Alexa';
        const modified = convertot(text);
        console.log(text);
        console.log(modified);

        【讨论】:

          猜你喜欢
          • 2014-02-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-23
          • 1970-01-01
          • 2023-03-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多