javascript - 为什么我的密码总是只正确加密第一个字母而不是第二个、第三个字母等

标签 javascript html

所以我一直在制作这个密码,现在如果我输入'a'并移动1,它会输出'b',这是正确的,但如果我输入'ab'或任何其他长度的字符串它例如,输出为“bl”。我该如何解决这个问题?

function encryption(str, amount) {
  var str = document.getElementById('inputText').value;
  var amount = document.getElementById('amountDropdown').value;
  var result = "";
  //loop through each character in word
  for (var i = 0; i < str.length; i++) {
    var c = str.charCodeAt(i);
    //If the character is uppercase
    if ((c >= 65) && (c <= 90)) {
      result += String.fromCharCode(((c - 65 + amount) % 26) + 65);
    }
    //if the character is lowercase
    else if ((c >= 97) && (c <= 122)) {
      result += String.fromCharCode(((c - 97 + amount) % 26) + 97);
    }
    //if the character isn't a letter
    else {
      result += str.charAt(i);
    }
  }
  //return the result
  outputResult.innerHTML = "The text you have input has been encrypted to.." + result;
}

function decryption() {
  var str = document.getElementById('inputText').value;
  var amount = document.getElementById('amountDropdown').value;
  var result = "";
  //loop through each character in word
  for (var i = 0; i < str.length; i++) {
    var c = str.charCodeAt(i);
    //If the character is uppercase
    if (c >= 65 && c <= 90) {
      result += String.fromCharCode((c - 65 - amount) % 26 + 65);
    }
    //if the character is lowercase
    else if (c >= 97 && c <= 122) {
      result += String.fromCharCode((c - 97 - amount) % 26 + 97);
    }
    //if the character isn't a letter
    else {
      result += str.charAt(i);
    }
  }
  //return the result
  outputResult.innerHTML = "The text you have input has been encrypted to.." + result;
}

function encryptClickHandler() {
  document.getElementById('inputText').value = encrypt(document.getElementById('inputText').value, document.getElementById('amount').value);
}

function decryptClickHandler() {
  document.getElementById('inputText').value = decrypt(document.getElementById('inputText').value, document.getElementById('amount').value);
}

最佳答案

输入字段的值始终是字符串。

当您计算(c - 97 - amount) % 26时,您会得到以下结果:

c = 97, amount = 1    => (97 - 97 + 1)   % 26 = (0 + 1)   % 26 = 1    % 26 = 1
c = 98, amount = 1    => (98 - 97 + 1)   % 26 = (1 + 1)   % 26 = 2    % 26 = 2
c = 97, amount = '1'  => (97 - 97 + '1') % 26 = (0 + '1') % 26 = '01' % 26 = 1
c = 98, amount = '1'  => (98 - 97 + '1') % 26 = (1 + '1') % 26 = '11' % 26 = 11

要解决此问题,请将值转换为整数:

var amount = parseInt(document.getElementById('amountDropdown').value, 10);

关于javascript - 为什么我的密码总是只正确加密第一个字母而不是第二个、第三个字母等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37035962/

相关文章:

javascript - AJAX 窗口无法使用西里尔字母 - 为什么?

javascript - 将鼠标悬停在某个区域上时使用 jQuery 在文本框中滑动

javascript - 创建嵌套表格行

javascript - 简单的 html5 网页无法识别 Jquery

javascript - 为什么我的 javascript 没有循环

javascript - 边框效果就像 Windows 10 中的 css3/html5

javascript - 从 C# UWP 处理 js 按钮

JavaScript 动画卡住,仅显示最终结果

javascript - 将变量设置为 'undefined' 实际上会释放空间吗?

html - Div 不会移动到它应该移动的位置