Created
August 10, 2023 08:49
-
-
Save pickaxe828/bbd86afd92a5088296a535282e24f0d3 to your computer and use it in GitHub Desktop.
Caesar cipher written in JavaScript. This only works for A-Z. Lowercase and symbols is not shifted intentionally.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This only works for A-Z. Lowercase and symbols will not be shifted. | |
let str = "UAMG! VJKU QPNA YQTMU HQT C-B." | |
let shift = -2 | |
let arr = Array.from(str).map((item) => item.charCodeAt(0)) | |
arr = arr.map((item) => { | |
if (item >= 65 && item <= 90) { // is A-Z | |
let temp = item + shift | |
if (!(temp >= 65 && temp <= 90)) { // result out of bound | |
if (shift < 0) { return temp + 26 } // if shift is neg | |
else { return temp - 26 } | |
} else { // result in bound | |
return temp | |
} | |
} else { // not in A-Z, return the original value | |
return item | |
} | |
}) | |
console.log( | |
arr.map((item) => String.fromCharCode(item)).join("") | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment