繁体   English   中英

JavaScript动态创建的对象未定义

[英]JavaScript Dynamically created object undefined

我正在做freecodecamp算法挑战“ Caesars Cipher”。 我的代码有问题。 我尝试将查找表生成为动态对象,并且由于某种原因它没有注册。 执行console.log时,提示“查找表未定义”。 与Acode变量相同。 如果我注释掉console.logs,它将起作用,但由于下面的部分将检查struprr中的char是否在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);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM