Tidied Substitute

This commit is contained in:
n1474335 2022-11-25 11:23:32 +00:00
parent 743b834f6d
commit d7561ec208

View file

@ -60,9 +60,10 @@ class Substitute extends Operation {
// convert using the dictionary keeping the case of the input character // convert using the dictionary keeping the case of the input character
if (dict[char] !== undefined) if (dict[char] !== undefined) {
// if the character is in the dictionary return the value with the input case // if the character is in the dictionary return the value with the input case
return isUpperCase ? dict[char].toUpperCase() : dict[char].toLowerCase(); return isUpperCase ? dict[char].toUpperCase() : dict[char].toLowerCase();
}
// check for the other case, if it is in the dictionary return the value with the right case // check for the other case, if it is in the dictionary return the value with the right case
if (isUpperCase) { if (isUpperCase) {
@ -84,21 +85,24 @@ class Substitute extends Operation {
*/ */
run(input, args) { run(input, args) {
const plaintext = Utils.expandAlphRange([...args[0]]), const plaintext = Utils.expandAlphRange([...args[0]]),
ciphertext = Utils.expandAlphRange([...args[1]]); ciphertext = Utils.expandAlphRange([...args[1]]),
ignoreCase = args[2];
let output = ""; let output = "";
const ignoreCase = args[2];
if (plaintext.length !== ciphertext.length) if (plaintext.length !== ciphertext.length) {
output = "Warning: Plaintext and Ciphertext lengths differ\n\n"; output = "Warning: Plaintext and Ciphertext lengths differ\n\n";
}
// create dictionary for conversion // create dictionary for conversion
const dict = {}; const dict = {};
for (let i = 0; i < Math.min(ciphertext.length, plaintext.length); i++) for (let i = 0; i < Math.min(ciphertext.length, plaintext.length); i++) {
dict[plaintext[i]] = ciphertext[i]; dict[plaintext[i]] = ciphertext[i];
}
// map every letter with the conversion function // map every letter with the conversion function
for (const character of input) for (const character of input) {
output += this.cipherSingleChar(character, dict, ignoreCase); output += this.cipherSingleChar(character, dict, ignoreCase);
}
return output; return output;
} }