• + 0 comments

    2 javascript solutions.

    Tracking double indexes, not too pleasant to read:

        for (let i = 0, j = s.length-1; i < s.length-1; i++, j--) {
            const [code, codeNext] = [s.charCodeAt(i), s.charCodeAt(i+1)];
            const [reverseCode, reverseCodeNext] = [s.charCodeAt(j), s.charCodeAt(j-1)];
    
            if (Math.abs(code- codeNext) !== Math.abs(reverseCode - reverseCodeNext)){
                return "Not Funny";
            }
        }
    
        return "Funny";
    

    An easier to read solution

    function getDiff (str: string): number[] {
        return Array(str.length-1).fill(0).map((_, i) => {
            const code = str.charCodeAt(i);
            const codeNext = str.charCodeAt(i+1);
            return Math.abs(code - codeNext);
        })
    }
    
    function funnyString(s: string): string {
        // Write your code here
    
        const diff = getDiff(s);
        const reverseDiff = getDiff(s.split("").reverse().join(""));
        
        if (diff.some((el, i)=> el !== reverseDiff[i])) {
            return "Not Funny";
        }
        return "Funny";
    }