• + 0 comments

    Javascript

    function encryption(s) {
        const noSpaces = s.replaceAll(' ', '');
        const strLen = noSpaces.length;   
        const row = Math.floor(Math.sqrt(strLen));
        const col = Math.ceil(Math.sqrt(strLen)); 
     
        const encryptedArr = [];
        let pos = 0;
        for(let i=0; i<strLen; i++) {
            const str = noSpaces.substr(i, 1);
            
            encryptedArr[pos] = encryptedArr[pos] ?? '';
            encryptedArr[pos] += str;
            
            pos = pos === col-1 ? 0 : pos+1;
        }
        
        return encryptedArr.join(' ');
    }