At school, we have to write a function that would encrypt a certain message by moving each letter of the message by an 'nC' (or niveauCryptage) number in the alphabet.
Currently, whenever I execute the code, I am stuck on a forever loading screen after completing both of the user interactions (the ones that ask the number by which the letters will be shifted in the alphabet & the message the user wants to encrypt). I suspect that something is wrong with the loops.
function crypter(t, l, nC) {
//This function receives the alphavet Table, a letter, and the number by which the letter has to be shifted in the alphabet.
//This function returns a single crypted letter...
var crypte = '';
var i = 0;
while (t[i] != l) {
i++;
}
if (i + nC > t.length) {
crypte = (i + nC) - (t.length);
} else {
crypte = i + nC;
}
return crypte;
}
//Global variables
var tAlphaB = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' '
];
var niveauCryptage = 0;
var texteBase = '';
var tTexteAvant = new Array();
var tTexteApres = [];
var txt = 'Le résultat du cryptage est : \n\n';
//Main program
//Ask the user to specify the number by which the letters of the message have to be shifted in the alphabet
//This number has to be a number between 1 and 10 (1 and 10 included)
do {
niveauCryptage = parseFloat(prompt("Saisissez un nombre entre 1 et 10 pour selectionner le niveau de cryptage"));
} while (isNaN(niveauCryptage) || niveauCryptage > 10 || niveauCryptage < 1)
//Ask the user to enter a message on the condition that it is a chain of characters
do {
texteBase = prompt("Quel texte voudrez-vous crypter?");
} while (!isNaN(texteBase))
//Transform the message into the table tTexteAvant using the split('') function
tTexteAvant = new Array(texteBase.split(''));
//Encrypt each letter of the table, tTexteAvant, into a new table, tTexteApres
var j = 0;
while (j < tTexteAvant.length) {
tTexteApres.push(crypter(tAlphaB, tTexteAvant[j], niveauCryptage));
j++;
}
The tTexteAvant = new Array(texteBase.split('')); will create an array with a single item which will be the array with the characters.
You most likely want tTexteAvant = texteBase.split('');
As stated in the comment of Jesse, this part in the crypter function goes on infinite loop when the character l is not in the array, that is when it is a lowercase or punctuation character, for instance.
while (t[i] != l) {
i++;
}
If you just need to find if an element of a string is present in an array you might replace that part with an array function that finds the value and returns the index in the array (or -1 if not found):
i = t.indexOf(l);
Then, you should decide what to do with the invalid or unrecognized characters or prompt the user to enter a valid string (only uppercase alphabet characters and spaces). You could also decide to transform any lowercase characters to uppercase, if this is acceptable.
As a side note, check the return value of the crypter function: it returns the new index and not the new letter. As commented at the beginning of the function, this is not the expected behaviour.