Created
January 10, 2020 07:44
[Hackerrank] Solution of Encryption Shop in JavaScript - nguyenhungkhanh.com
This file contains hidden or 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
// 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