动态创建对象未定义
JavaScript Dynamically created object undefined
我正在做freecodecamp算法挑战"Caesars Cipher"。我的代码有问题。我试图生成一个查找表作为一个动态对象,由于某种原因,它没有注册。当执行console.log时,它说"查找表未定义"。Acode变量也是如此。如果我注释掉控制台。然后它将工作,但它不会加密任何东西,因为下面的部分检查strArr中的字符是否存在于lookupTable中,如果不存在,它应该将相同的值分配给encryptedArr(这样做是为了不加密逗号,空格等):
strArr.forEach(function(thisArg) {
var newValue;
if(lookupTable[thisArg] !== undefined ) {
newValue = lookupTable[thisArg];
} else {
newValue = thisArg;
}
encryptedArr.push(newValue);
});
当然lookupTable[thisArg]总是未定义的。下面是包含上述部分的整个函数:
function rot13(str) { // LBH QVQ VG!
var strArr;
var encryptedArr = [];
var Acode;
var lookupTable = {}; //this object will contain the mapping of letters
var encryptedString;
//check the code of A , this will be a reference for the first letter as the algorith will use Modular Arithmetic
Acode = 'A'.charCodeAt(0);
console.log(Acode);
//generate an object containing mappings (I din't want to do it initially but theoreticaly just making lookups in a table would be more efficiant for huge workloads than calculating it every time)
//this algorithm is a little bit complecated but i don't know how to do modular arithmetic in code properly so I use workarrounds. If a = 101 then I do 101 + the remainder from current letter((Acode + 1) - 13) divided by 26 which works
for (i = 0; i < 26; i++) {
lookupTable[String.fromCharCode(Acode + i)] = String.fromCharCode(Acode + ((Acode + i) - 13) % 26);
console.log(lookupTable[String.fromCharCode(Acode + i)]);
}
//save the string into the array
strArr = str.split("");
//change letters into numbers and save into the code array
strArr.forEach(function(thisArg) {
var newValue;
if (lookupTable[thisArg] !== undefined) {
newValue = lookupTable[thisArg];
} else {
newValue = thisArg;
}
encryptedArr.push(newValue);
});
encryptedString = encryptedArr.join("");
return encryptedString;
}
// Change the inputs below to test
rot13("SERR PBQR PNZC");
console.log(Acode);
我做错了什么与lookupTable对象的创建和与以下?
Acode = 'A'.charCodeAt(0);
没有未定义变量。代码的问题在于如何计算查找表项。您的代码将每个字符映射到自身,而不是移动13。正确的公式是
Acode + ((i + 13) % 26)
Acode
是字母的ASCII码,在执行模移位时不应该包括它。你只需要将模数应用于从字母表开始移动13后的偏移量。
function rot13(str) { // LBH QVQ VG!
var strArr;
var encryptedArr = [];
var Acode;
var lookupTable = {}; //this object will contain the mapping of letters
var encryptedString;
//check the code of A , this will be a reference for the first letter as the algorith will use Modular Arithmetic
Acode = 'A'.charCodeAt(0);
// console.log(Acode);
//generate an object containing mappings (I din't want to do it initially but theoreticaly just making lookups in a table would be more efficiant for huge workloads than calculating it every time)
//this algorithm is a little bit complecated but i don't know how to do modular arithmetic in code properly so I use workarrounds. If a = 101 then I do 101 + the remainder from current letter((Acode + 1) - 13) divided by 26 which works
for (i = 0; i < 26; i++) {
lookupTable[String.fromCharCode(Acode + i)] = String.fromCharCode(Acode + ((i + 13) % 26));
// console.log(lookupTable[String.fromCharCode(Acode + i)]);
}
//save the string into the array
strArr = str.split("");
//change letters into numbers and save into the code array
strArr.forEach(function(thisArg) {
var newValue;
if (lookupTable[thisArg] !== undefined) {
newValue = lookupTable[thisArg];
} else {
newValue = thisArg;
}
encryptedArr.push(newValue);
});
encryptedString = encryptedArr.join("");
return encryptedString;
}
// Change the inputs below to test
var result = rot13("SERR PBQR PNZC");
console.log(result);
相关文章:
- 从模块函数返回未定义的对象
- 未捕获的类型错误: 无法读取未定义的 - 对象的属性
- 未定义 RequireJS 对象
- 未检索到输入字段的值:未定义的对象
- 未定义Javascript对象Class错误
- 如何在不获得引用错误的情况下将不存在的JavaScript对象与未定义的对象进行比较
- Javascript - 在未定义的对象上调用 new
- 如何删除未定义的对象错误并根据位置重定向用户
- 无论如何,在Visual Studio智能感知中定义一个未定义的对象
- 余烬数据断言失败:无法对未定义的对象使用“查询”调用 get
- 角度控制器/工厂返回未定义的对象
- 无法读取未定义的对象
- 对于循环添加被读取为未定义的对象
- javascript:未定义获取对象
- 汇总.js:外部依赖项中未定义的对象
- 即使未调用 JQuery 未定义的对象属性仍会导致错误
- getJSON JavaScript 函数返回未定义的对象
- 帆查询不断呈现未定义的对象
- 未定义的对象值和NaN
- 未定义Javascript对象