字符串 - 元音的元音,字母表中的下一个字母 - Javascript

Pan*_*Bus 1 javascript string

我正在尝试将字符串的所有字母转换为字母表的后续字母,例如A应该变为B,X应该变为Y,Z应该变为A等.

我想在完成字母转换后将每个元音都大写.

function LetterChanges(str) {

   var c = str.split("");
    var vowels = ["a", "e", "i", "o", "u"]; 
   if (c == vowels) {
      vowels.toUpperCase();}
   if (c == "z") return "a";  
    return str.replace(/[a-z]/gi, function(s) {
      return String.fromCharCode(s.charCodeAt(c)+1);
      });

}
LetterChanges("cold buttz"); 
Run Code Online (Sandbox Code Playgroud)

元音部分和za部分不能正常工作.请帮忙?

elc*_*nrs 6

看看这是否有帮助:

var str = 'cold buttz';

str = str.replace(/[a-z]/gi, function(char) {
  char = String.fromCharCode(char.charCodeAt(0)+1);
  if (char=='{' || char=='[') char = 'a';
  if (/[aeiuo]/.test(char)) char = char.toUpperCase();
  return char;
});

console.log(str); //= "dpmE cvUUA"
Run Code Online (Sandbox Code Playgroud)

编辑:我可以看到你的代码在我的上一个答案中有点混乱/粘贴......以下是对它的错误的简要描述:

function LetterChanges(str) {

  var c = str.split(""); // array of letters from `str`
  var vowels = ["a", "e", "i", "o", "u"]; // array of vowels

  // `c` and `vowels` are two different objects
  // so this test will always be false
  if (c == vowels) {
    // `toUpperCase` is a method on strings, not arrays
    vowels.toUpperCase();
  }

  // You're comparing apples to oranges,
  // or an array to a string, this test will also be false
  // Then you return 'a'?? This was meant to be inside the `replace`
  if (c == "z") return "a";

  // OK, I see you recycled this from my other answer
  // but you copy/pasted wrong... Here you're basically saying:
  // "For each letter in the string do something and return something new"
  return str.replace(/[a-z]/gi, function(s) { // `s` is the letter
    // Here we find out the next letter but
    // `c` is an array and `charCodeAt` expects an index (number)
    return String.fromCharCode(s.charCodeAt(c)+1);

    // `.charCodeAt(0)` gives you the code for the first letter in a string
    // in this case there's only one.
  });
}
Run Code Online (Sandbox Code Playgroud)