M
M
max20202020-01-30 13:20:51
JavaScript
max2020, 2020-01-30 13:20:51

How can I solve this problem using Javascript?

Please tell me the following.
There is a key: 18 and an encrypted message: What is the best way to decrypt it?
Only one way came to my mind.
Are there options to solve this with es6?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Klimenko, 2020-01-30
@max2020

It seems that the author wants to see how the function can be rewritten in a more functional style. For example, like this:

function convertText(str, key) {
    key = (26 - key) % 26;

    return [...str].map(symb => {
        const code = symb.charCodeAt(0);
        let shift = 0;

        if ((65 <= code) && (code <= 90)) {
            shift = 65;
        } else if ((97 <= code) && (code <= 122)) {
            shift = 97;
        }
        
        if (shift > 0) {
            return String.fromCharCode(shift + ((code - shift + key) % 26));
        } else {
            return symb;
        }
    }).join('');
}

alert(convertText('Hmjw uzsfuw dwv eq xjawfv Zwjumdw Hgajgl, xgjewjdq uzawx gx lzw Twdyasf xgjuw, lg tw ugffwulwv oalz lzw Klqdwk uskw', 18));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question