此代码旨在识别给定一组可能的字谜的字符串的字谜数组。
var anagram = function(input) {
return input.toLowerCase();
}
我在这里将matcher函数添加到String原型中。
String.prototype.matcher = function(remainingLetters) {
var clone = this.split("");
for (var i = 0; i < clone.length; i++) {
if (clone[i].indexOf(remainingLetters) > -1) {
remainingLetters.splice(clone[i].indexOf(remainingLetters, 1));
clone.splice(i, 1);
}
}
if (remainingLetters.length == 0 && clone.length == 0) {
return true;
}
else {
return false;
}
}
a
String.prototype.matches = function(matchWordArray) {
var result = [];
for (var i = 0; matchWordArray.length; i++) {
var remainingLetters = this.split("");
if (matchWordArray[i].matcher(remainingLetters)) {
result.push(arrayToMatch[i]);
}
}
return result;
}
var a = anagram("test");
a.matches(["stet", "blah", "1"]);
module.exports = anagram;
答案 0 :(得分:0)
应该是:
for (var i = 0; i < matchWordArray.length; i++) {
原声明:
for (var i = 0; matchWordArray.length; i++) {
...会导致无限循环,因为matchWordArray.length在测试中始终是真实的(3)。