2013-08-03 20 views
1

我經歷了CoderByte練習,我碰到下面的問題就來了:更改字母算法,工作在JSBIN但不是在Coderbyte,尋求澄清

>使用JavaScript語言,具備的功能LetterChanges(STR )使用傳遞的str參數並使用以下算法對其進行修改。將字符串中的每個字母替換爲字母后面的字母(即c變成d,z變成a)。然後在這個新字符串(a,e,i,o,u)中大寫每個元音,最後返回這個修改過的字符串。

我在JSBin中寫出來,它工作正常(甚至te,但在CoderByte它沒有。我想問社區,如果我寫的是正確的,這是CoderByte上的問題,或者如果我的代碼是錯誤的,問題是JSBin

的代碼如下:。

function LetterChanges(str) { 
    var iLetters = str.split(''); 
    var newStr = []; 

    for (var i = 0; i < str.length; i++) { 
     if (/[a-y]/ig.test(iLetters[i])) { 
      newStr[i] = String.fromCharCode(iLetters[i].charCodeAt(0) + 1); 
      if (/[aeiou]/ig.test(newStr[i])) { 
       newStr[i] = newStr[i].toUpperCase(); 
      } 
     } else if (/[z]/ig.test(iLetters[i])) { 
      newStr[i] = "A"; 
     } else if (/[^A-Z]/ig.test(iLetters[i])) { 
      newStr[i] = iLetters[i]; 
     } 
    } 

    return newStr.join(''); 
} 
+0

看起來像他們的後端JS亞軍是錯誤的,無論是或控制檯中的錯誤,雖然它看起來不相關。 –

+1

謝謝,我很感激。伸出我的JS肌肉進行一次技術性採訪,這讓我更加強調自己的方式。 – DefionsCode

回答

1

好像在他們的後端JS亞軍的錯誤確實如你所說,你的代碼運行正常並且應該被接受,值得報告給他們支持imo。

下面是一個替代的解決方案specifying a function as second parameter.replace():當針對以下替代

在CoderByte相比,你的代碼失敗,但下面的工作

function LetterChanges(str) { 
    return str.replace(/[a-z]/ig, function(c) { 
    return c.toUpperCase() === 'Z' ? 'A' : String.fromCharCode(c.charCodeAt(0) + 1); 
    }).replace(/[aeiou]/g, function(c) { 
    return c.toUpperCase(); 
    }); 
} 
+0

+1我愛你的答案是一個明智的答案。我必須說,因爲你的回答,我必須刷掉我的正則表達式技能(仍然需要了解更多)。但看看我的解決方案。沒有得到這部分** c.toUpperCase()==='Z'? 'A':** – Danish

+0

@丹麥語它是「z變成」的一部分。 'toUpperCase'應用於以不區分大小寫的方式執行此操作(z - > A,Z - > A)。 「A」字符總是大寫,因爲該算法最後大寫所有元音。 –

+0

謝謝你的回答。由於算法總是最後大寫元音,而且程序已經用'String.fromCharCode(c.charCodeAt(0)+ 1)'替換了下一個單詞,所以,我想我們真的需要「z變成」一個部分。歡呼 – Danish

0

你的代碼工作得很好,我對jsfiddle。似乎是他們網站上的問題。

function letterChanges(str) { 
    var newString = "", 
     code, 
     length, 
     index; 

    for (index = 0, length = str.length; index < length; index += 1) { 
     code = str.charCodeAt(index); 
     switch (code) { 
      case 90: 
       code = 65; 
       break; 
      case 122: 
       code = 97 
       break; 
      default: 
       if ((code >= 65 && code < 90) || (code >= 97 && code < 122)) { 
        code += 1; 
       } 
     } 

     newString += String.fromCharCode(code); 
    } 

    return newString.replace(/[aeiou]/g, function (character) { 
     return character.toUpperCase(); 
    }); 
} 

console.log(LetterChanges("Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string.")); 
console.log(letterChanges("Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string.")); 

輸出

UIfO dbqjUbmjAf fwfsz wpxfm jO UIjt Ofx tUsjOh (b, f, j, p, v) bOE gjObmmz sfUvsO UIjt npEjgjfE tUsjOh. fiddle.jshell.net/:70 
UIfO dbqjUbmjAf fwfsz wpxfm jO UIjt Ofx tUsjOh (b, f, j, p, v) bOE gjObmmz sfUvsO UIjt npEjgjfE tUsjOh. 
0

從@回答法布里西奧磨砂 ,有點解釋只是另一種解決方案是使用正則表達式使用/[a-z]/獲得第一字母從A到Z,並通過添加一個替換它們每個字符串的ASCII使用String.fromCharCode(Estr.charCodeAt(0)+1),剩下的就是發現元音再次使用正則表達式[aeiou]並返回大寫的字符串。

function LetterChanges(str) { 
    return str.replace(/[a-z]/ig, function(Estr) { 
     return String.fromCharCode(Estr.charCodeAt(0)+1); 
    }).replace(/[aeiou]/ig, function(readyStr) { 
     return readyStr.toUpperCase(); 
    }) 
}