Caesar solution: Review my code and give your feedback

Guys, I finally came up with this solution after spending two and a half days on it.

I’m so happy that I was able to solve this challenge without looking at the solution. The code seems a bit verbose though :wink:

Please review my code and give your feedback.

const caesar = function(str, num) {
    let arr = str.split('');
    let shift = num % 26;
    let shiftPositive = Math.abs(shift);

    const unicodeArray = arr.map(item => item.match(/[a-zA-Z]+/g) ? item.charCodeAt() : item);

    const finalArray = unicodeArray.map(item => {
        if (typeof item === 'number' && shift === 0) { // no shifting
            return item;

        } else if (typeof item === 'number' && shift > 0) { // shifting right

            if (item > 64 && item < 91) { // for UpperCase items
                item = item + shiftPositive;

                if (item > 90 && item < 116) {
                    return item - 26;
                } else {
                    return item;
                }

            } else if (item > 96 && item < 123) { // for LowerCase items
                item = item + shiftPositive;

                if (item > 122 && item < 148) {
                    return item - 26;
                } else {
                    return item;
                }
            }


        } else if (typeof item === 'number' && shift < 0) { // shifting left

            if (item > 64 && item < 91) { // for UpperCase items
                item = item - shiftPositive;

                if (item > 39 && item < 65) {
                    return item + 26;
                } else {
                    return item;
                }

            } else if (item > 96 && item < 123) { // for LowerCase items
                item = item - shiftPositive;

                if (item > 71 && item < 97) {
                    return item + 26;
                } else {
                    return item;
                }
            }

        } else {
            return item;
        }
    });

    const messageArray = finalArray.map(item => {
        if (typeof item === 'number') {
            return item = String.fromCharCode(item);
        } else {
            return item;
        }
    });

    return messageArray.join('');
    
}

I’m no expert in algorithms, but I do think you’re overthinking the code. You don’t need to test for if something is a number over and over if you limit the input to certain ASCII values. You can see my solution here if you want to compare: That said, mine was for FCC and I think that was case insensitive, but you could just add two more ifs to the transform function to handle the capital letter ranges (or add extra conditions to the ifs).