DEV Community

loading...

Discussion on: Daily Challenge #235 - Reversing a Process

Collapse
_bkeren profile image
'' • Edited

"Not correcly chosen" should be clarified.

ES6


  const alphabet = "abcdefghijklmnopqrstuvwxyz"

  const decode = (encoded) => {
    let number = parseInt(encoded.split(/\D/)[0])
    let text = encoded.substring(("" + number).length)
    let result = ""
    for(let i=0;i<text.length;i++) {
        let encodedIndex = alphabet.indexOf(text[i])
        for(let j=0;j<26;j++) {
            if((j*number - encodedIndex) % 26 === 0) {
                result += alphabet[j]
                break;
            }
        }
    }

    return result;
}