I'm trying to make Ceaser cipher that shifts the input by the value you chose, for example you chose 3, and the program will shift letters by 3 to the back, so i created an array type variable alpha that contains letters from a to z, and create two other variable one is the shift value and the other is a string filled by the user, my idea was to compare each letter from the input with alpha and if they match then i will replace it with the shifted value.
input[i] = alpha[j-shift_value];
i wanted to do it as simple as possible but for some reason it's not working, I'll provide a code snippet.
var ClearText = document.getElementById("txt_clr").value;
var DecValue = document.getElementById("valeur_dec").value;
var alpha = "abcdefghijklmnopqrstuvwxyz";
var longeur = ClearText.length;
// alpha[0]=a;
// alpha[26]=z;
var i;
function decalager() {
for (i = 0; i < longeur; i++) {
for (let j = 0; j < 26; j++) {
if (ClearText[i] == alpha[j]) {
ClearText[i] = alpha[j - DecValue];
}
}
}
console.log(ClearText);
document.getElementById("resultat").innerHTML = ClearText;
}
body {
height: 100vh;
width: 100vw;
justify-content: center;
align-items: center;
margin: 0;
background-color: #002b36;
display: flex;
}
.centrer {
margin: auto;
}
p {
color: wheat;
font-size: 18px;
font-style: italic;
}
textarea {
border: 1px black solid;
border-radius: 15px;
padding: 5px;
}
button,
input {
height: 50px;
width: 113px;
border: 0;
border-radius: 15px;
}
<html>
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
<div class="centrer">
<p>Décalage: </p> <input id="valeur_dec" type="text"> <button onclick="decalager()">Chiffrer</button>
<p>Texte claire: </p>
<textarea id="txt_clr" cols="30" rows="10"></textarea>
<p>Texte avec Décalage: </p>
<textarea id="resultat" cols="30" rows="10"></textarea>
</div>
</body>
</html>