我们正在进行一项任务,在这个任务中,我们必须生成代码,在游戏中扮演一个玩家,牛和公牛。我正在处理一个基于codeSize和codeChars值生成代码的函数。CodeSize是代码的大小,CodeChars是alpha还是数字,这取决于用户所选择的内容。然而,我似乎无法骑上这些复制品。
// Check if we are not allowed to include duplicate characters:
if (nodupes === true) {
// True if character already used:
var added = (used.indexOf(chr) !== -1);
// Skip the Character if already used:
if (added === true) {
continue;
}
// Add Character to the list of used ones:
used += chr;
// alert(used);
}
// Add Character to the output string:
output += chr;
// alert(output);
}
while (output.length < codeSize);
// alert(typeof(output));
// alert(output);
return output; // Not working Correctly string still has duplicate characters.
if (output.length == codeSize) {
result = output;
alert(result);
}
}发布于 2016-03-03 16:09:26
首先,让我们清理注释和缩进,因为这里的内容是不可读的。
首先修复压痕..。
// Check if we are not allowed to include duplicate characters:
if (nodupes === true) {
// True if character already used:
var added = (used.indexOf(chr) !== -1);
// Skip the Character if already used:
if (added === true) {
continue;
}
// Add Character to the list of used ones:
used += chr;
// alert(used);
}
// Add Character to the output string:
output += chr;
// alert(output);
}
while (output.length < codeSize);
// alert(typeof(output));
// alert(output);
return output; // Not working Correctly string still has duplicate characters.
if (output.length == codeSize) {
result = output;
alert(result);
}
}这完全没有意义。
它以前没有,但现在我们至少可以看到它有什么问题:
这些是主要的问题。
然而,第二个问题是致命的;这是错误的代码。特别是对于注释"//不能正确工作“,字符串仍然有重复字符。
https://codereview.stackexchange.com/questions/118321
复制相似问题