字母→下一个字母和大写元音 [英] Letter → next letter and capitalize vowels

查看:201
本文介绍了字母→下一个字母和大写元音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个代码似乎仍然没有工作。它已经没有错误,但它只返回像这样的 {} 的空括号。它应该把 str 中的每个字母变成下一个字母,并大写每个元音。任何想法?
$ b $ pre $ 函数LetterChanges(str){
str = str.split(); // split ()字符串到数组
for(var i = 0; i< str.length; i ++){// for循环检查每个字母
if(str [i] .match(/ [ay] / i)){
str [i] = String.fromCharCode(str [i] .charCodeAt(0)+1);
} else if(str [i] .match(z)){
str [i] =a; $ str $ {
}
if(str [i] .match(/ [aeiou] / i)){
str [i] = str [i] .toUpperCase
}

}
str = str.join();
//通过在字母表中加入修饰字母
//将每个元音大写
// join()字符串


return str;


解决方案

简化为只需几次调用 <$ c

$ p $ lt; code>函数LetterChanges(str){
返回str
.replace(/ [ay] |(z)/ gi,function(c,z){return z?'a':String.fromCharCode(c.charCodeAt(0)+1);} )
.replace(/ [aeiou] / g,function(c){return x.toUpperCase();});
}

LetterChanges(abcdefgxyz);
//bcdEfghyzA

或者,对 .replace ,像这样:

$ p $函数LetterChanges(str){
return str .replace(/(z)|([dhnt])| [ay] / gi,函数(c,z,v){
c = z?'A':String.fromCharCode(c.charCodeAt +1);
return v?c.toUpperCase():c;
})
}

LetterChanges(abcdefgxyz);
//bcdEfghyzA


This code still doesn't seem to be working. It has no errors anymore, but it only returns blank brackets like this {}. It is supposed to turn each letter in str into the next letter and capitalize each vowel. Any ideas?

function LetterChanges(str) { 
str = str.split("");//split() string into array
  for(var i=0;i<str.length;i++){//for loop that checks each letter
    if(str[i].match(/[a-y]/i)){
      str[i]=String.fromCharCode(str[i].charCodeAt(0)+1);
        }else if(str[i].match("z")){
          str[i] = "a";
        }
    if(str[i].match(/[aeiou]/i)){
       str[i] = str[i].toUpperCase();
       }

  }
   str = str.join("");
  //modifies letter by adding up in alphabet
  //capitalizes each vowel
  //join() string


  return str; 
}

It looks like this method can by simplified to just a few calls to .replace:

function LetterChanges(str) { 
    return str
        .replace(/[a-y]|(z)/gi, function(c, z) { return z ? 'a' : String.fromCharCode(c.charCodeAt(0)+1); })
        .replace(/[aeiou]/g, function(c) { return x.toUpperCase(); });
}

LetterChanges("abcdefgxyz"); 
//            "bcdEfghyzA"

Or alternatively, a single call to .replace, like this:

function LetterChanges(str) { 
    return str.replace(/(z)|([dhnt])|[a-y]/gi, function(c, z, v) {
        c = z ? 'A' : String.fromCharCode(c.charCodeAt(0)+1); 
        return v ? c.toUpperCase() : c; 
    })
}

LetterChanges("abcdefgxyz"); 
//            "bcdEfghyzA"

这篇关于字母→下一个字母和大写元音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆