Created
October 17, 2021 18:29
-
-
Save lior-amsalem/b33a3702958c99bcf3fe310de5619221 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
Developer comment | |
- This is a pseudo-encryption algorithm which given a string S and an integer N concatenates all the odd-indexed | |
characters of S with all the even-indexed characters of S, this process should be repeated N times. | |
- use recursive function | |
- decress the N | |
- every step encrypt the text | |
- make sure we have exist statement, than return the text | |
**/ | |
function encrypt(text, n) { | |
if(n <= 0 || text === null) { | |
return text; | |
} | |
let arrayOfText = text.split(''); | |
let leftArray = arrayOfText.filter((word,index) => index%2); | |
let rightArray = arrayOfText.filter((word,index) => !(index%2)); | |
text = leftArray.join('') + rightArray.join(''); | |
n--; | |
return encrypt(text, n); | |
} | |
function decrypt(encryptedText, n) { | |
if(n <= 0 || encryptedText.trim() === '') { | |
return encryptedText; | |
} | |
let cutAt = Math.floor(encryptedText.length/2); | |
let leftArray = encryptedText.split('').splice(0,cutAt); | |
let rightArray = encryptedText.split('').splice(cutAt); | |
let newar = []; | |
rightArray.map((value, key) => { | |
newar.push(value) | |
newar.push(leftArray[key]); | |
}); | |
newar = newar.join(''); | |
n--; | |
return decrypt(newar, n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment