Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Last active February 24, 2024 18:00
Show Gist options
  • Save sandrabosk/803fc22d31e17338bc702ca27cc91911 to your computer and use it in GitHub Desktop.
Save sandrabosk/803fc22d31e17338bc702ca27cc91911 to your computer and use it in GitHub Desktop.
// ************************************************************************************
// https://www.codewars.com/kata/5848565e273af816fb000449/javascript
// Description:
// Encrypt this!
// You want to create secret messages which can be deciphered by the Decipher this! kata.
// Here are the conditions:
// Your message is a string containing space separated words.
// You need to encrypt each word in the message using the following rules:
// The first letter needs to be converted to its ASCII code.
// The second letter needs to be switched with the last letter
// Keepin' it simple: There are no special characters in input.
// // Examples:
// encryptThis("Hello") === "72olle"
// encryptThis("good") === "103doo"
// encryptThis("hello world") === "104olle 119drlo"
// ************************************************************************************
const encryptThis = text => {
return text
.split(' ')
.map(e => {
if (e.length === 1) return e.charCodeAt(0);
if (e.length === 2) return `${e[0].charCodeAt(0)}${e[1]}`;
if (e.length === 3) return `${e[0].charCodeAt(0)}${e.slice(-1)}${e[1]}`;
if (e.length > 3) return `${e[0].charCodeAt(0)}${e.slice(-1)}${e.slice(2, -1)}${e[1]}`;
})
.join(' ');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment