DEV Community

Discussion on: Reverse a string: awful answers only

Collapse
 
lionelrowe profile image
lionel-rowe • Edited

❤️ this challenge

This solution is inspired by my_first_calculator.py. It works for all lowercase alphabetic strings of length 3. The generated (non-evaled) version is 17,582 lines of code.

const reverse = eval(`str => {
${[...new Array(26).keys()].map(x =>
    [...new Array(26).keys()].map(y =>
        [...new Array(26).keys()].map(z =>
            [x, y, z]
                .map(n => String.fromCharCode(n + 'a'.charCodeAt(0)))
        )
    )
)
    .flat()
    .flat()
    .map(([x, y, z]) =>
        `    if (str === '${[x, y, z].join('')}') return '${[z, y, x].join('')}'`)
    .join('\n')}

    throw new RangeError(
        'Sorry, your string is not supported yet. Please try again in 3–6 months.'
    )
}`)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dwighthaul_52 profile image
Paul Hubert

When I open the "my_first_calculator.py" file it took so long, I thought something was wront with the git site ou my internet was down. Then I saw the file and I understood