Files
2025-02-22 03:37:47 -07:00

27 lines
729 B
JavaScript

export function caesarEncrypt(text, shift) {
text = text.split("");
var alphabet = [ '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 output = "";
text.forEach(function(c) {
if(c == " ") {
output += " ";
} else {
var letter = alphabet.indexOf(c) - shift;
if(letter < 0) {
letter = alphabet.length + letter;
}
output += alphabet[letter];
}
});
return output;
}
/** @param {NS} ns */
export async function main(ns) {
var text = ns.args[0];
var shift = ns.args[1];
ns.writePort(1, encrypt(text, shift));
}