Skip to content

Instantly share code, notes, and snippets.

@IrhaAli
Created October 15, 2022 14:05
Show Gist options
  • Save IrhaAli/4ac102fa0d20a94e4de9d38b78b86c21 to your computer and use it in GitHub Desktop.
Save IrhaAli/4ac102fa0d20a94e4de9d38b78b86c21 to your computer and use it in GitHub Desktop.
Returns the provided message in square code
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