-
-
Save IrhaAli/4ac102fa0d20a94e4de9d38b78b86c21 to your computer and use it in GitHub Desktop.
Returns the provided message in square code
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
const squareCode = function(message) { | |
let codedMessage = ''; | |
let unspacedMessage = message.replaceAll(' ', ''); | |
let messageLength = unspacedMessage.length; | |
let cols = Math.ceil(Math.sqrt(messageLength)); | |
let rows = Math.ceil(messageLength / cols); | |
for (let i = 0; i < cols; i++){ | |
for (let j = 0; j < rows; j++){ | |
if ((i+cols*j) < messageLength){ | |
codedMessage += unspacedMessage[i+cols*j]; | |
} | |
} | |
codedMessage += ' '; | |
} | |
return codedMessage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment