I need help to decrypt string from “51Pa0Lp0e” to “aP1pL5e”.
i can explain how it is encrypted:
1.Initially i=0.
2.if s[i] is lowercase and the next character s[i+1] is uppercase, swap them , add a ‘*’ after them,and move to i+2.
3.if s[i] is a number,replace it with 0,place the original number at the start, and move to i+1.
4.else, move to i+1.
5.stop if i is more than or equal to the string length. Otherwise, goto step2.
the input or encrypted string =“43Ah*cK0rr0nK” and the output or decrypted string should be"hAcK3rr4nK"
below is code i have tried and i dont konw how to reverse two letters in string:
function decryptPassword(s) {
/ /remove *
let find= '*';
let replace = '';
while( s.indexOf(find) > -1)
{
var s = s.replace(find, replace);
}
//replace the number
let i=0, r=s.slice(0,2);
while( s.indexOf('0') > -1)
{
var s = s.replace('0', r[i]);
r=r[i+1]
console.log(s)
}
//reverse letters back
s = s.slice(2,9);
console.log(s)
s=s.split('', 2).reverse().join("");
console.log(s)
}
var s="51Pa*0Lp*0e"
decryptPassword(s)