Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daubattu/a0f3f3aae72d3f94c1caed7faf1553b1 to your computer and use it in GitHub Desktop.
Save daubattu/a0f3f3aae72d3f94c1caed7faf1553b1 to your computer and use it in GitHub Desktop.
[Hackerrank] Solution of Encryption Shop in JavaScript - nguyenhungkhanh.com
// Complete the encryption function below.
function encryption(s) {
const ceil = Math.ceil(Math.sqrt(s.length));
let temp = s;
let array = [];
while(temp) {
array = array.concat(temp.substring(0, ceil));
temp = temp.substring(ceil)
}
let result = [];
for(let i = 0 ; i < ceil; i++) {
result = result.concat(
array.reduce((r, v) => {
return r + (v[i] || "")
}, "")
)
};
return result.join(' ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment