I am trying to encode a string of words the user gives using ASCII. Doing it by each character. But the random number keeps changing insetead of using the same number to encode.
I want all the Uppercase letters to be the same number/character.
const rand = generateRandom(65, 90);
const randomNum = rand;
function generateRandom(min, max)
{
let rand = Math.floor(Math.random() * (max - min) + 1) + min;
return rand;
}
function encodeIt()
{
document.getElementById("message").innerHTML = ("<h2> </h2>");
var msg = prompt("Enter your message." , " ");
let newmsg = " ";
var upCaseCode = 155;
var newCode = 0;
var lowCaseCode = 219;
var specialCode = 3;
//the loop encodes each letter in the message string
for (var j = 0; j < msg.length; j++)
{
//check for lowercase letters and encode them
if ((msg.charCodeAt(j)>=97) && (msg.charCodeAt(j)<=122))
{
newcode = (lowCaseCode - msg.charCodeAt(j));
}
else
//check for numbers and special characters and encode them33.
if (((msg.charCodeAt(j)>90) && (msg.charCodeAt(j)<97)) || (msg.charCodeAt(j)<65))
{
newcode = (msg.charCodeAt(j) + specialCode);
}
//add each encoded character to the new message
newmsg = newmsg + " " + String.fromCharCode(newcode);
}
//display the encoded message on the web page
document.getElementById("secret").innerHTML = ("<h2>" + newmsg + "</h2>");
//decide if original message should be shown
var choice = prompt("Do you want the special key? Yes or No?", " ");
if ((choice.charAt(0) == 'y') || (choice.charAt(0) == 'Y'))
{
document.getElementById("message").innerHTML = ("<h2>" + randomNum + "</h2>");
}
}
//check for upppercase letters and encode them
if ((msg.charCodeAt(j)>=65) && (msg.charCodeAt(j)<=90))
{
newcode = (**randomNum **- msg.charCodeAt(j));
}
else
Aucun commentaire:
Enregistrer un commentaire