gpt4 book ai didi

JavaScript动态创建对象未定义

转载 作者:行者123 更新时间:2023-12-03 05:46:13 25 4
gpt4 key购买 nike

我正在做 freecodecamp 算法挑战赛“Caesars Cipher”。我的代码有问题。我尝试生成一个查找表作为动态对象,但由于某种原因它没有注册。当执行 console.log 时,它显示“查找表未定义”。 Acode 变量也是如此。如果我注释掉 console.logs 那么它会工作,但它不会加密任何东西,因为下面的部分检查 strArr 中的字符是否存在于 LookupTable 中,如果不存在,它应该为 cryptoArr 分配相同的值(这是已完成,不加密逗号、空格等):

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);

最佳答案

没有 undefined variable 。您的代码的问题在于如何计算查找表条目。您的代码将每个字符映射到其自身,而不是移动 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);

关于JavaScript动态创建对象未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40325084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com