Created
March 20, 2022 22:06
-
-
Save see-why/ccf8fc7cf09db87fa91ef40efab05e20 to your computer and use it in GitHub Desktop.
HackerRank The Love Letter Mystery challenge solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.hackerrank.com/challenges/the-love-letter-mystery/problem?isFullScreen=true | |
function theLoveLetterMystery(s) { | |
// Write your code here | |
let operations = 0; | |
for (let i=0; i<s.length/2; i++){ | |
let char = s.charCodeAt(i) | |
let alternate = s.charCodeAt(s.length - (1 + i)) | |
if(char != alternate) { | |
operations+= Math.abs(char-alternate) | |
} | |
} | |
return operations; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment