Created
July 3, 2020 03:20
-
-
Save andreiskandar/88b21fce511d722f6db84f895eeab571 to your computer and use it in GitHub Desktop.
Create a function named squareCode that will receive a message, and return the secret square code version of the message.
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
/* | |
Create a function named squareCode that will receive a message, | |
and return the secret square code version of the message. | |
*/ | |
const squareCode = function(message) { | |
let len = Math.ceil(Math.sqrt(message.split(' ').join("").length)); | |
const messageWithoutSpace = message.split(' ').join(''); | |
let encrypted = ''; | |
for(let i = 0; i < len; i++){ | |
for(let j = 0; j < len; j++){ | |
if(messageWithoutSpace[len * j + i] !== undefined) { | |
encrypted += messageWithoutSpace[len * j + i] | |
} else encrypted += ""; | |
} | |
encrypted += " " | |
} | |
return encrypted | |
}; | |
console.log(squareCode("chill out")); | |
// console.log(squareCode("feed the dog")); | |
// console.log(squareCode("have a nice day")); | |
// console.log(squareCode("if man was meant to stay on the ground god would have given us roots")); | |
/* Expected Output | |
clu hlt io | |
fto ehg ee dd | |
hae and via ecy | |
imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment