最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

String - Vowels to uppercase, letters to next in alphabet - Javascript - Stack Overflow

programmeradmin3浏览0评论

I'm trying to convert all the letters of the string to the following letter of the alphabet, e.g. A should bee B, X should bee Y, Z should bee A etc.

I want to capitalize every vowel after the letter shifting is done.

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"); 

The vowels part and the z to a part is not working. Please help?

I'm trying to convert all the letters of the string to the following letter of the alphabet, e.g. A should bee B, X should bee Y, Z should bee A etc.

I want to capitalize every vowel after the letter shifting is done.

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"); 

The vowels part and the z to a part is not working. Please help?

Share Improve this question edited Jun 10, 2017 at 12:46 Black 20.4k46 gold badges185 silver badges296 bronze badges asked Jul 4, 2013 at 6:15 PanicBusPanicBus 5761 gold badge7 silver badges17 bronze badges 3
  • Just curious: are you trying a generate a password using the input? – devnull Commented Jul 4, 2013 at 6:17
  • @devnull Nope. Just doing an exercise. Self-educating myself, rather poorly it seems. – PanicBus Commented Jul 4, 2013 at 16:58
  • 2 @Patashu, that's not helpful. – PanicBus Commented Jul 4, 2013 at 16:59
Add a ment  | 

2 Answers 2

Reset to default 6

See if this helps:

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"

Edit: I can see your code was sort of a messy copy/paste from my last answer... Here's a brief description of what's wrong with it:

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 paring 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.
  });
}

My solution does exactly what you asked for. The letters are first shifted in the alphabet and then the vowels are uppercased.

Have a look:

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!!"));

发布评论

评论列表(0)

  1. 暂无评论