JavaScript动态创建的对象未定义

时间:2016-10-30 00:50:59

标签: javascript caesar-cipher

我正在进行freecodecamp算法挑战“Caesars Cipher”。我的代码有问题。我尝试生成一个查找表作为动态对象,由于某种原因它不会注册。在执行console.log时,会说“查找表未定义”。它与Acode变量相同。如果我注释掉console.logs然后它会工作,但它不会加密任何东西,因为下面的部分检查来自strArr的char是否存在于lookupTable中,如果没有,它应该为encryptedArr分配相同的值(这是完成不加密逗号,空格等):

strArr.forEach(function(thisArg) {
    var newValue;

    if(lookupTable[thisArg] !== undefined ) {
      newValue = lookupTable[thisArg];
    } else {
      newValue = thisArg;
    }

    encryptedArr.push(newValue);

});

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

1 个答案:

答案 0 :(得分:1)

没有未定义的变量。代码的问题在于如何计算查找表条目。您的代码将每个字符映射到自身,而不是移动13.正确的公式是

Acode + ((i + 13) % 26)

Acode是该字母的ASCII代码,在执行模块化转换时,您不应该包含该代码。您只想将模数转换为13后将模数应用于字母表开头的偏移量。

&#13;
&#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);
&#13;
&#13;
&#13;