The Love-Letter Mystery

  • + 0 comments

    My answer with Typescrippt, simple

    function theLoveLetterMystery(s: string): number {
        let operations = 0
    
        // Calculate the sum of the differences between opposite pairs of characters (number ascii).
        for (let i = 0; i < Math.floor(s.length / 2); i++) {
            let reverse_i = s.length - i - 1
            if (s[i] != s[reverse_i]) {
                operations += Math.abs(s.charCodeAt(i) - s.charCodeAt(reverse_i))
            }
        }
    
        return operations
    }