【发布时间】:2014-11-04 04:54:53
【问题描述】:
对于那些熟悉这个测验的人,我试图接受一个字符串参数并将每个字母转换为字母表中的字母。例如,参数“abc”应该变成“bcd”。
我的代码的第一部分有效。它采用参数的第一个字母并将其转换。现在我正在尝试对参数的每个字母执行此操作,然后将结果连接成一个字符串作为输出。这部分不起作用。我收到错误消息,“SyntaxError: Unexpected token ;”
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而改变。
标签: javascript arrays counter