首页 > 解决方案 > eventlistener 不更新全局变量

问题描述

我正在尝试将 ascii 转换为 char 并将 char 转换为 ascii 翻译器

const outerWheel = document.querySelector('.outer-wheel');
const innerWheel = document.querySelector('.inner-wheel');
const centerWheel = document.querySelector('.wheel-center');
let key = document.querySelector('.key');

key.addEventListener('input', function(e){
  let key = e.target.value
  outerWheel.style.transform = `translate(-50%, -50%) rotate(0deg)`
  innerWheel.style.transform =  `translate(-50%, -50%) rotate(${key * 14.4}deg)`
  centerWheel.style.transform = `translate(-50%, -50%) rotate(0deg) rotate(${key * -14.4}deg)`
})


let shift = 0
let str = ''
let strBox = document.querySelector('.encrypt-text')
let shiftBox = document.querySelector('.encrypt-key')
shiftBox.addEventListener('input', function (e)  {
   shift = e.target.value // this is supposed for shifting the ascii codes
})
console.log(shift)
strBox.addEventListener('input', function(e){
  let str = e.target.value // i want this value to calculate the end of the for loop 
console.log(str)

 
  let codedStr = '';
  for (let i = 0; i < str.length; i++) {
  let char = str.charAt(i)
  let ascii = char.charCodeAt(0)

  let shiftedAscii = ascii + shift
  switch(true) {
    case (ascii >= 65 && ascii <= 90):
      if (shiftedAscii < 65) {shiftedAscii += 26} 
      if (shiftedAscii > 90) {shiftedAscii -= 26} 
      break;
    case (ascii >= 97 && ascii <= 122):
      if (shiftedAscii < 97) {shiftedAscii += 26} 
      if (shiftedAscii > 122) {shiftedAscii -= 26} 
      break;
    default:
      console.log('nem betu')
      break;  
  } 
  let codedChar = String.fromCharCode(shiftedAscii)
  codedStr += codedChar;
}
codedStr= codedStr.substr(codedStr.length - str.length)
const encryptedText = document.querySelector('.encrypted-text')
encryptedText.innerHTML = codedStr

标签: javascriptglobal-variablesaddeventlistenerevent-listener

解决方案


推荐阅读