對於那些熟悉這個測驗,我試圖採取一個字符串參數,並將每個字母轉換爲字母表中的字母。 EG,參數「abc」應該變成「bcd」。CoderByte JavaScript字母更改 - 第一步
我的代碼的第一部分工作。它採用參數的第一個字母並將其轉換。現在我正在嘗試爲參數的每個字母執行此操作,然後將結果連接成一個字符串作爲輸出。這部分不起作用。我收到錯誤「SyntaxError:意外的令牌;」
function LetterChanges(str) {
var string = str.toLowerCase()
var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
var n = 0;
var output = "";
var currentLetter = string[n];
var currentAlphaPos = alphabet.indexOf(currentLetter);
var nextAlphaPos = currentAlphaPos + 1;
var nextAlpha = alphabet[nextAlphaPos];
//the code above this works. The code below results in an error
while (i = 1;i < string.length; i++){
output += nextAlpha;
n += 1;
};
return output;
}
我是初學者,所以在此先感謝。
你永遠不會更新currentLetter,currentAlphaPos,nextAlphaPos或nextAlpha。這些值不會因爲您更改了'n'而改變。 – Mathletics 2014-11-04 05:56:07