Simple string scrambling Task: Take every 2nd char from the string, then the other chars, that are not every 2nd char, and concat them as new String.
Do this n times!

Examples:

"This is a test!", 1 -&gt; "hsi  etTi sats!"
"This is a test!", 2 -&gt; "hsi  etTi sats!" -&gt; "s eT ashi tist!"


Code:


const split = (text) =&gt; {
  const firstPart = text.split('').filter((element, index) =&gt; index % 2 != 0).join('')
  const secondPart = text.split('').filter((element, index) =&gt; index % 2 == 0).join('')

  return firstPart + secondPart
}

const encrypt = (text, n) =&gt; {
  if (text == null) {
    return null
  }

  let result = text

  for (let i = 0; i &lt; n; i++) {
    result = split(result)
  }

  return result
}

const decrypt = (text, n) =&gt; {
  if (text == null) {
    return null
  }

  for (let i = 0; i &lt; n; i++) {
    let firstPartLength = text.length % 2 == 0 ? text.length / 2 : (text.length - 1) / 2 + 1
    let secondPartLenght = text.length % 2 == 0 ? text.length / 2 : (text.length - 1) / 2
    let firstPart = text.substring(0, firstPartLength)
    let secondPart = text.substring(secondPartLenght, text.length)

    secondPart = secondPart.split('').map(element =&gt; element + ' ').join('')
    let position = -1
    secondPart = secondPart.split('').map((element, index) =&gt; index % 2 != 0 ? firstPart[++position] : element).join('').substring(0, text.length)

    text = secondPart
  }

  return text
}

console.log(decrypt(encrypt('This is a test!', 3), 3))

console.log(encrypt('Some text!', 4))
const split = (text) =&gt; (text.replace(/.(.)?/g, '$1') + ("d"+text).replace(/.(.)?/g, '$1')


The regex matches every other char. Do it twice, add any arbitrary char to the start of the string the second time to offset it.