Hi all, I have just completed the Message Mixer project and here below is my code:
Encryptros.js
// Declare and export the functions here.
const caesarCipher = (str, amount = 0) => {
if (amount < 0) {
return caesarCipher(str, amount + 26);
}
let output = '';
for (let i = 0; i < str.length; i++) {
let char = str[i];
if (char.match(/[a-z]/i)) {
let code = str.charCodeAt(i);
if (code >= 65 && code <= 90) {
char = String.fromCharCode(((code - 65 + amount) % 26) + 65);
} else if (code >= 97 && code <= 122) {
char = String.fromCharCode(((code - 97 + amount) % 26) + 97);
}
}
output += char;
}
return output;
};
const symbolCipher = (str) => {
const symbols = {
'i': '!',
'!': 'i',
'l': '1',
'1': 'l',
's': '$',
'$': 's',
'o': '0',
'0': 'o',
'a': '@',
'@': 'a',
'e': '3',
'3': 'e',
'b': '6',
'6': 'b'
}
let output = '';
for (let i = 0; i < str.length; i++) {
let char = str.toLowerCase()[i];
if (symbols[char]) {
output += symbols[char]
} else {
output += char;
}
}
return output;
}
const reverseCipher = (sentence) => {
let words = sentence.split(' ');
for (let i = 0; i < words.length; i++) {
words[i] = words[i].split('').reverse().join('');
}
return words.join(' ');
};
/*
module.exports.caesarCipher = caesarCipher;
module.exports.symbolCipher = symbolCipher;
module.exports.reverseCipher = reverseCipher;
*/
module.exports = {
caesarCipher,
symbolCipher,
reverseCipher
}
super-encoder.js
// Import the encryptors functions here.
const encryptors = require("./encryptors.js");
const caesarCipher = encryptors.caesarCipher;
const symbolCipher = encryptors.symbolCipher;
const reverseCipher = encryptors.reverseCipher;
const encodeMessage = (str) => {
// Use the encryptor functions here.
return reverseCipher(symbolCipher(caesarCipher(str, 6)));
};
const decodeMessage = (str) => {
// Use the encryptor functions here.
return reverseCipher(symbolCipher(caesarCipher(str, -6)));
};
// User input / output.
const handleInput = (userInput) => {
const str = userInput.toString().trim();
let output;
if (process.argv[2] === "encode") {
output = encodeMessage(str);
}
if (process.argv[2] === "decode") {
output = decodeMessage(str);
}
process.stdout.write(output + "\n");
process.exit();
};
// Run the program.
process.stdout.write("Enter the message you would like to encrypt...\n> ");
process.stdin.on("data", handleInput);
Now the functionality of super-encoder is fine and it returns no errors ( after few tries and editing of the code ). The issue is that when I prompt the command decode " urrkn jrxuc " (“” not included) it returns back h3110 w0rld. see the screenshot below:
Any reason why it would throw this error?
I don’t think is something to do with my code but with the logic of the super-ecryptors logic which was already written in the project, but I would love to understand