Password decryption-javascript

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)

Welcome, meghanar.

You have everything you need to get the solution, but, to make your life easier, I would, from the very beginning, split the string into an array. Work from there…


Also, I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like