Проблемы с формулой Цезаря Шифра, включающей преобразование ASCII - PullRequest
0 голосов
/ 16 января 2019

После использования изменений из моего последнего вопроса я столкнулся с другой проблемой, используя формулу для использования шифра Цезаря: если буква x и смещение n :

En (x) = (x + n) мод. 26;

Однако, когда я попытался перенести это из моей предыдущей Python-программы, полностью защищенной от ошибок, он не дал того же результата. Вместо abcdefghijklmnopqrstuvwxyz -> defghijklmnopqrstuvwxyzabc он создает abcdefghijklmnopqrstuvwxyz -> defghijklmnopklmnopqrstuvw. В чем проблема с кодом JS?

var shfBox = document.getElementById("shfBox");
var strBox = document.getElementById("strBox");
var button = document.getElementById("button");

button.addEventListener("click", function(){
  var orgstr = String(strBox.value);
  var orgshf = +Number(shfBox.value);
  var str = orgstr;
  var shf = orgshf;
  var output = "";

  for (var i=0; i < str.length; i++) {
    var asciiValue = str[i].charCodeAt();
    if (asciiValue >= 65 && asciiValue <= 77) {
      output += String.fromCharCode((asciiValue + shf - 65) % 26 + 65);

    } else if (asciiValue >= 78 && asciiValue <= 90) {
      output += String.fromCharCode((asciiValue + shf - 65) % 26 + 65);

    } else if (asciiValue >= 97 && asciiValue <= 109) {
      output += String.fromCharCode((asciiValue + shf - 97) % 26 + 97);

    } else if (asciiValue >= 110 && asciiValue <= 122) {
      output += String.fromCharCode((asciiValue - shf - 97) % 26 + 97);

    } else {
      output += str[i];
    }
  }
  
  document.getElementById("output").innerHTML = output;
  return output;
  return str;
});
*{
  box-sizing: border-box;
  margin: 0px;
  padding: 0px;
}


body {
  font-family: Courier;
}


#mainNav {
  font-family: Courier;
  display: flex;
  justify-content: space-between;
  padding: 0px 16px;
  position: fixed;
  width: 100%;
  top: 0px;
  background: #eee;
}

section:nth-of-type(1) {
  margin-top: 84px;
  font-family: Courier;
  height: 220px;
  padding: 16px;
  font-size: 17px;
}

section:nth-of-type(2) {
  font-family: Courier;
  height: 110px;
  padding: 16px;
  font-size: 17px;

}


section:nth-of-type(3) {
  font-family: Courier;
  height: 300px;
  padding: 16px;
  font-size: 17px;
}


footer {
  height: 70px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: darkslategray;
  color: white;
}

#mainNav ul {
  list-style: none;
  display: flex;
  align-items: center;
  width: 400px;
  justify-content: space-around;

}

.button {
  background-color: lightblue;
  font-family: Courier;
  border: none;
  color: white;
  padding: 15px 25px;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
}

.button:hover {
  background-color: skyblue;
}



#mainNav ul li a {
  text-decoration: none;
  font-weight: 500;
  color: #333;
}
<!DOCTYPE html>

  
    
    
    repl.it
    
  
  
    
a. Encrypted Phrase
      
      
      
      
б. Номер смены
      
      <textarea id="shfBox" rows="2" cols="50"></textarea>
      
      
с. Оригинал сообщения
        
      <button class="button" id="button">Decipher</button>
      
      <p>Deciphered Code:</p>

      <p id="output"></p>
      
Это некорректный сайт.

1 Ответ

0 голосов
/ 16 января 2019

У вас есть - вместо + в вашем последнем else if утверждении. Кроме того, я не совсем уверен, почему у вас есть четыре разных случая вместо двух, так как они, кажется, делают то же самое. Вот как это может работать:

for (var i=0; i < str.length; i++) {
  var asciiValue = str[i].charCodeAt();
  if (asciiValue >= 65 && asciiValue <= 90) {
    output += String.fromCharCode((asciiValue + shf - 65) % 26 + 65);

  } else if (asciiValue >= 97 && asciiValue <= 122) {
    output += String.fromCharCode((asciiValue + shf - 97) % 26 + 97);

  } else {
    output += str[i];
  }
}
...