2013-07-04 216 views
1

我試圖將字符串的所有字母轉換爲下一個字母,例如: 一個應該成爲X應該成爲Ÿž應該成爲一個字符串 - 大寫元音,下一個字母的字母 - Javascript

我想要的字母轉換完成後,以大寫每個元音。

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

的元音部分和za部分不能正常工作。請幫忙?

+0

只是好奇:你想生成一個使用輸入密碼嗎? – devnull

+2

幾乎函數中的每一行都寫入不正確。 – Patashu

+0

@devnull沒有。只是做一個練習。對自己進行自我教育,看起來很糟糕。 – PanicBus

回答

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" 

編輯:我可以看到你的代碼是有點兒凌亂的複製/粘貼從我last answer ......這裏是什麼地方錯了簡要說明:

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

你可能應該解釋他的代碼是怎麼錯的,以及你爲什麼工作,因爲他是JS的新手。 –

+0

@Derek朕會功夫:不幸的是,代碼非常混亂,但我會嘗試...請參閱編輯。 – elclanrs

+0

非常棒,@elclanrs。這真的很有幫助,謝謝。 – PanicBus

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

相關問題