DEV Community

Marius
Marius

Posted on

 

JS practice: day2 - password decoder

Yesterday I wrote a script for generating passwords based on a priming word. It's a basic function that uses Regex to match vowels and consonants. Next, it ads each into separate arrays, then does some counting, addition, and multiplication on the initial numbers. Ultimately, it scrambles it all in a single array which I then join('') into the returned string.

It's not rocket science, but it's fun and useful. Today I wrote a little script to decode these passwords; let's get into it.

// the RegExp patten that matches all letters and one digit after
let decodeRegex = new RegExp('[a-z][0-9]','gi');

const decodePW = (c) => {

  let chainInitial = [], chainArranged = [];
  // initial array containing pairs of letters and indexes 
  chainInitial = c.match(decodeRegex);

  chainInitial.map((el) => { 
    // get a var that contains only numbers
    let index = Number(el.substring(1))
    // get a var that contains only letters
    let letters = el.substring(0,1)
    // reassign indexes in the array
    chainArranged[index] = letters
  });

  // make it a string
  let primedWord = chainArranged.join('').toLowerCase()
  return primedWord
};

console.log(decodePW(pwString));
Enter fullscreen mode Exit fullscreen mode

And this is pretty much it. This script has a limitation; the priming word needs to be a maximum of ten characters; otherwise, the decoding will not work. That is due to the Regex pattern that I use here let decodeRegex = new RegExp('[a-z][0-9]','gi');. It will select only one number after the letter. Feel free to comment if you can make it work with longer words.

Thanks for reading.

Top comments (0)

Visualizing Promises and Async/Await 🤯

async await

☝️ Check out this all-time classic DEV post