如何将 if/else 语句更改为在 javascript 中循环

我想将 if/else 语句更改为在接收字符串并返回一个新字符串的函数中循环,该字符串将元音字母更改为它之后的下一个字母。


function changeVowels(str) {

        var alphabet = 'abefijopuvABEFIJOPUV';

        var strNew = ""

        for(var i = 0; i < str.length; i++){

           for(var j = 0; j < alphabet.length; j++){

             if(str[i] === alphabet[j] && j%2 === 0){

               strNew += alphabet[j+1]

            }

          }

          if(str[i]==='a' || str[i]==='i' || str[i]==='u'||str[i]==='e' || str[i]==='o'){}

          else if(str[i]==='A' || str[i]==='I' || str[i]==='U'|| str[i]==='E' || str[i]==='O'){}

          else{

           strNew+=str[i]

          }

        }

        return strNew

      }


console.log(changeVowels('Car'))

如何将下面的 if/else 语句更改为循环?


if(str[i]==='a' || str[i]==='i' || str[i]==='u'||str[i]==='e' || str[i]==='o'){}

else if(str[i]==='A' || str[i]==='I' || str[i]==='U'|| str[i]==='E' || str[i]==='O'){}

else{strNew+=str[i]}

或者我不介意你在循环之外改变它,或者让它更短



临摹微笑
浏览 191回答 3
3回答

猛跑小猪

您可以检查字符是否是元音,然后从给定的字符串或实际字母中取出下一个字母。function changeVowels(str) {&nbsp; var alphabet = 'abefijopuvABEFIJOPUV';&nbsp; var strNew = ""&nbsp; for (var i = 0; i < str.length; i++) {&nbsp; &nbsp; if ('aeiou'.includes(str[i].toLowerCase())) {&nbsp; &nbsp; &nbsp; strNew += alphabet[alphabet.indexOf(str[i]) + 1];&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; strNew += str[i];&nbsp; &nbsp; }&nbsp; }&nbsp; return strNew;}console.log(changeVowels('Car'))

一只名叫tom的猫

这是另一种有趣的方法function changeVowels(str) {&nbsp; &nbsp; var x = {a:'b', e:'f', i:'j', o: 'p', u: 'v', A: 'B', E: 'F', I: 'J', O: 'P', U: 'V'};&nbsp; &nbsp; return str.split('').map(l => x[l] || l).join('');}console.log(changeVowels('Car'))或者如果你真的想要那个alphabetvarfunction changeVowels(str) {&nbsp; &nbsp; var alphabet = 'abefijopuvABEFIJOPUV';&nbsp; &nbsp; var x = Object.fromEntries(alphabet.match(/../g).map(kv => kv.split('')));&nbsp; &nbsp; return str.split('').map(l => x[l] || l).join('');}console.log(changeVowels('Car'))的解释 var x = Object.fromEntries(alphabet.match(/../g).map(kv => kv.split('')));alphabet.match(/../g) 产生一个数组 ['ab', 'ef', 'ij', ... etc].map(kv => kv.split('')) 将上面的映射到 [['a', 'b'], ['e', 'f'], ... etcObject.fromEntries([['a', 'b'], ['e', 'f'] .... etc]) 产生{&nbsp; &nbsp; a: 'b',&nbsp; &nbsp; e: 'f',&nbsp; &nbsp; i: 'j',&nbsp; &nbsp; o: 'p',&nbsp; &nbsp; u: 'v',&nbsp; &nbsp; A: 'B',&nbsp; &nbsp; E: 'F',&nbsp; &nbsp; I: 'J',&nbsp; &nbsp; O: 'P',&nbsp; &nbsp; U: 'V'};与第一个片段相同

ibeautiful

您不需要循环来简化此代码这两个str[i]==='a' || str[i]==='i' || str[i]==='u'||str[i]==='e' || str[i]==='o'和str[i]==='A' || str[i]==='I' || str[i]==='U'|| str[i]==='E' || str[i]==='O'大约相同的字母,但大小写不同,所以你可以只考虑一个情况下(上限或下限)和转换什么减少一半你的代码。如果提取str[i]为变量,则可以进一步减少重复:var char = str[i].toUpperCase();/* ... */if (char==='A' || char==='I' || char==='U'|| char==='E' || char==='O') {}如果您使用数组和Array#includes或者String#includes将执行相同的操作,则可以进一步减少多个 OR,但我将使用数组,因为它对值更通用,即使字符串会更短。if (['A', 'I', 'U', 'E', 'O'].includes(char)) {&nbsp; /* code */} else {&nbsp; strNew+=str[i];}如果你想在第一种情况下什么都不做,你可以else通过反转条件来进一步消除:if (!['A', 'I', 'U', 'E', 'O'].includes(char)) {//&nbsp; ^ invert the result&nbsp; strNew+=str[i];}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript