作为我创建的拼写检查算法的一部分,我编写了一个函数,该函数返回所有单词的数组,这些单词具有从给定单词替换的1个字母。字母替换必须在键盘上距离1;因此我制作了以下地图
var kdmap = {'a': ['q','w','s','z'],
'b': ['v','g','h','n'],
'c': ['x','d','f','v'],
'd': ['s','e','r','f','c','x'],
'e': ['w','s','d','r'],
'f': ['d','r','t','g','v','c'],
'g': ['f','t','y','h','b','v'],
'h': ['g','y','u','j','n','b'],
'i': ['u','h','j','i'],
'j': ['h','n','m','k','i','u','y'],
'k': ['j','i','o','l','m'],
'l': ['k','o','p'],
'm': ['n','j','k'],
'n': ['b','h','j','m'],
'o': ['i','k','l','p'],
'p': ['o','l'],
'q': ['a','w'],
'r': ['e','d','f','t'],
's': ['a','w','e','d','x','z'],
't': ['r','f','g','y'],
'u': ['y','h','j','i'],
'v': ['c','f','g','b'],
'w': ['q','a','s','e'],
'x': ['z','s','d','c'],
'y': ['t','g','h','u'],
'z': ['a','s','x']
};
用于以下功能。
function letter_replacements(w)
{
/* Given a word w, return an array of all strings that are w
with 1 letter replaced, that letter being one which is distance
1 away on the keyboard
*/
var A = new Array();
for (var i = 0, j = w.length; i < j; ++i)
{
var thisChar = w[i];
var closeChars = kdmap[thischar];
for (var k = 0, n = closeChars.length; k < n; ++k)
A.push(w.slice(0,i)+closeChars[k]+w.slice(i+1,j))
}
return A;
}
当我运行包含该代码的页面时,我的浏览器JavaScript控制台上没有错误。但是,当我创建一个小提琴来测试函数时,我没有得到任何输出。
这是我的小提琴:http://jsfiddle.net/5hQyp/
知道我做错了吗?
答案 0 :(得分:2)
对我来说,看起来这两行代码存在问题:
var thisChar = w[i];
var closeChars = kdmap[thischar];
必须:
var thisChar = w[i];
var closeChars = kdmap[thisChar.toLowerCase()];
if (closeChars === undefined)
continue;
1)你的外壳已关闭。 thisChar 不是 thischar 。
2)你不会考虑空格字符。所以closeChars可能是未定义的。
3)你必须在你获得的每个角色上调用toLowerCase,因为你的JavaScript对象中没有大写字母字符。
<强> FIDDLE 强>
答案 1 :(得分:0)
也许你需要编写一个程序来检查案例;)
查看控制台,您会看到错误:Uncaught ReferenceError: thischar is not defined
查看你搞砸了的代码。
var thisChar = w[i];
^^^
var closeChars = kdmap[thischar];
^^^
答案 2 :(得分:0)
我已经更新了你的小提琴一些修复。我将详细说明以下问题:
您不能保证最终得到一个closeChars数组,所以:
var closeChars = kdmap [thisChar] || [];
您的地图仅涵盖小写字符:
var thisChar = w [i] .toLowerCase();
以下是修改后的实际代码。
var kdmap = {'a': ['q','w','s','z'],
'b': ['v','g','h','n'],
'c': ['x','d','f','v'],
'd': ['s','e','r','f','c','x'],
'e': ['w','s','d','r'],
'f': ['d','r','t','g','v','c'],
'g': ['f','t','y','h','b','v'],
'h': ['g','y','u','j','n','b'],
'i': ['u','h','j','i'],
'j': ['h','n','m','k','i','u','y'],
'k': ['j','i','o','l','m'],
'l': ['k','o','p'],
'm': ['n','j','k'],
'n': ['b','h','j','m'],
'o': ['i','k','l','p'],
'p': ['o','l'],
'q': ['a','w'],
'r': ['e','d','f','t'],
's': ['a','w','e','d','x','z'],
't': ['r','f','g','y'],
'u': ['y','h','j','i'],
'v': ['c','f','g','b'],
'w': ['q','a','s','e'],
'x': ['z','s','d','c'],
'y': ['t','g','h','u'],
'z': ['a','s','x']
};
function letter_replacements(w) {
/* Given a word w, return an array of all strings that are w
with 1 letter replaced, that letter being one which is distance
1 away on the keyboard
*/
var A = new Array();
for (var i = 0, j = w.length; i < j; ++i)
{
var thisChar = w[i].toLowerCase();
var closeChars = kdmap[thisChar] || [];
for (var k = 0, n = closeChars.length; k < n; ++k)
A.push(w.slice(0,i)+closeChars[k]+w.slice(i+1,j))
}
return A;
}
var myWord = "Obama smokes cigarettes.";
var myArray = letter_replacements(myWord);
document.getElementById("mydiv").innerHTML = "<p>" + myArray.join('') + "</p>";