JavaScript TypeError: Cannot read property 'eat' of Undefined
JavaScript TypeError: Cannot read property 'eat' of Undefined
我正在做一个项目,解决许多报纸上发现的经典 "jumbled word" 难题。该项目的基本思想是它接受一个乱序的单词(没有空格或特殊字符),生成单词的每个排列,根据我的教授提供的 "dictionary" 检查每个排列,然后添加每个排列实际上是一个英语单词到一个数组,然后进一步处理以得出结果。
目前,我 运行 遇到了一个问题,当我试图检查排列是否在 "dictionary" 中时出现了这个问题。下面的代码由我的教授提供,并从外部文本文件创建了一个 "dictionary"。根据他的说法,dictionary[w] 应该 return 一个数字与代表单词频率的单词配对,或者 "undefined" 如果单词不在字典中。
function readDictionary() {
/**
* @type {string[]}
*/
const lines = fs.readFileSync("count_1w.txt", "utf-8").split("\n");
var line;
const dictionary = {};
for (line of lines) {
line = line.trim();
let array = line.split('\t');
if (array.length > 1) {
let word = array[0];
let count = array[1];
if (lexicon[word]) {
dictionary[word] = parseFloat(count);
}
}
}
return Object.freeze(dictionary);
}
function getDictionary() {
if (dictionary === null) {
dictionary = readDictionary();
}
return dictionary;
}
var dictionary = getDictionary();
如果 dictionary[letters] 未定义,我编写的以下代码应该 return "true"...
function inDict(letters) {
if (dictionary[letters] !== undefined){
return true;
}
else{
return false;
}
}
...但是在其当前状态下,它会在此 post 的标题中抛出 TypeError,其中 'eat' 是生成的输入的第一个排列。注意,在我的实际代码中,readDictionary()、getDictionary()、var dictionary = getDictionary 都是在上面声明的inDict().
如果需要更多详细信息,请随时询问。我个人对 JavaScript 的知识已经穷尽,多次 Google 搜索对我的特定情况毫无帮助。非常感谢任何建议或意见!
错误消息很清楚:dictionary
的值为 undefined
,那是为什么?
问题是函数getDictionary
returnsundefined
。条件 dictionary === null
永远不会是 true
因为 dictionary
的初始值是 undefined
而 undefined === null
是 false
.
所以你真正做的是
var dictionary; // initial value is undefined
dictionary = dictionary;
什么都不做。
我完全不明白你为什么需要 getDictionary
。直接初始化dictionary
即可:
var dictionary = readDictionary();
或者您可以:
用null
初始化dictionary
(但你为什么要这么做?)
var dictionary = null;
dictionary = getDictionary();
与 undefined
比较:
function getDictionary() {
if (dictionary === undefined) {
dictionary = readDictionary();
}
return dictionary;
}
总体而言,getDictionary
设计不佳,因为它隐式依赖于 dictionary
,但也 returns 一个值。
我正在做一个项目,解决许多报纸上发现的经典 "jumbled word" 难题。该项目的基本思想是它接受一个乱序的单词(没有空格或特殊字符),生成单词的每个排列,根据我的教授提供的 "dictionary" 检查每个排列,然后添加每个排列实际上是一个英语单词到一个数组,然后进一步处理以得出结果。
目前,我 运行 遇到了一个问题,当我试图检查排列是否在 "dictionary" 中时出现了这个问题。下面的代码由我的教授提供,并从外部文本文件创建了一个 "dictionary"。根据他的说法,dictionary[w] 应该 return 一个数字与代表单词频率的单词配对,或者 "undefined" 如果单词不在字典中。
function readDictionary() {
/**
* @type {string[]}
*/
const lines = fs.readFileSync("count_1w.txt", "utf-8").split("\n");
var line;
const dictionary = {};
for (line of lines) {
line = line.trim();
let array = line.split('\t');
if (array.length > 1) {
let word = array[0];
let count = array[1];
if (lexicon[word]) {
dictionary[word] = parseFloat(count);
}
}
}
return Object.freeze(dictionary);
}
function getDictionary() {
if (dictionary === null) {
dictionary = readDictionary();
}
return dictionary;
}
var dictionary = getDictionary();
如果 dictionary[letters] 未定义,我编写的以下代码应该 return "true"...
function inDict(letters) {
if (dictionary[letters] !== undefined){
return true;
}
else{
return false;
}
}
...但是在其当前状态下,它会在此 post 的标题中抛出 TypeError,其中 'eat' 是生成的输入的第一个排列。注意,在我的实际代码中,readDictionary()、getDictionary()、var dictionary = getDictionary 都是在上面声明的inDict().
如果需要更多详细信息,请随时询问。我个人对 JavaScript 的知识已经穷尽,多次 Google 搜索对我的特定情况毫无帮助。非常感谢任何建议或意见!
错误消息很清楚:dictionary
的值为 undefined
,那是为什么?
问题是函数getDictionary
returnsundefined
。条件 dictionary === null
永远不会是 true
因为 dictionary
的初始值是 undefined
而 undefined === null
是 false
.
所以你真正做的是
var dictionary; // initial value is undefined
dictionary = dictionary;
什么都不做。
我完全不明白你为什么需要 getDictionary
。直接初始化dictionary
即可:
var dictionary = readDictionary();
或者您可以:
用null
初始化dictionary
(但你为什么要这么做?)
var dictionary = null;
dictionary = getDictionary();
与 undefined
比较:
function getDictionary() {
if (dictionary === undefined) {
dictionary = readDictionary();
}
return dictionary;
}
总体而言,getDictionary
设计不佳,因为它隐式依赖于 dictionary
,但也 returns 一个值。