动态创建对象未定义

JavaScript Dynamically created object undefined

本文关键字:未定义 创建对象 动态      更新时间:2023-09-26

我正在做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);