1

我正在尝试将字符串的所有字母转换为字母表中的以下字母,例如A应该变成BX应该变成YZ应该变成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"); 

元音部分和ztoa部分不起作用。请帮忙?

4

2 回答 2

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"

编辑:我可以看到你的代码是我上次回答的一个混乱的复制/粘贴......这是对它有什么问题的简要描述:

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.
  });
}
于 2013-07-04T06:28:40.770 回答
0

我的解决方案完全符合您的要求。字母首先在字母表中移动,然后元音大写。

看一看:

function LetterChanges(str) { 

    var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    var ret = new Array();
    
    for (var x=0; x < str.length; x++) {
            
        for(var i=0; i < alphabet.length; i++) {
            
            if (checkIfCharInString(alphabet, str[x]) == false) {
                ret[x] = str[x].toString();
                break;
            }

            if (str[x] == alphabet[i]) {
                if (alphabet[i] == "Z") {
                    ret[x] = "A";
                } else {
                    ret[x] = alphabet[i+1];
                }
            }
        }
    }
    
    var output = ret.join("");
    
    output = capitalizeVowels(output);

  // code goes here  
  return output; 
         
}

function checkIfCharInString(motherString, char)
{
    for(var i=0; i < motherString.length; i++) {
        if (motherString[i] == char.toString()) {
            return true;
        }
    }
    return false;
}

function capitalizeVowels(str)
{
    var vowel = "aeiou";
    var newStr = new Array();
    
    for(var i=0; i < str.length; i++) {
        for(var x=0; x < vowel.length; x++) {
          newStr[i] = str[i];
          
          if (str[i] == vowel[x]) {
              newStr[i] = vowel[x].toUpperCase();
              break;
          }
      }
    }

    return newStr.join("");
}
   
 
console.log(LetterChanges("Hello*3"));
console.log(LetterChanges("I love stackoverflow!"));
console.log(LetterChanges("I have Internet explorer!!"));

于 2017-06-10T12:37:40.577 回答