Encrypt string

Given a string, encrypt it by replacing sequences of same character with the character plus hexadecimal value of the length of the character. Return reversed string of the whole string. E.g., bbb will be 3b.

Hint

Count consecutive occurrences of the same character within the input string. Replace each sequence of repeating characters with the character itself plus the count of repetitions, converted to its hexadecimal representation. Use language's hex function for a decimal hex value. In Javascript, it is number's toString(16).

Reverse the entire encoded string and return it.

# Python implementation
data = 'abc'

output = ""
saved = None
count = 0

for i in range(len(data)):
  c = data[i]
  
  if c != saved:
    if saved != None:
      output += saved + hex(count)
    
    saved = c
    count = 0
    
  count += 1

output += saved + hex(count)
output = output.replace("0x", "")[::-1]

print(output)
// Javascript implementation
const data = 'abc';

let output = "";
let saved = null;
let count = 0

for (let i = 0; i < data.length; i++) {
  let c = data.at(i);
  
  if (c !== saved) {
    if (saved !==  null) {
      output += saved + count.toString(16);
    }
    saved = c;
    count = 0;
  }
    
  count++;
}

output += saved + count.toString(16);
output = output.split("").reverse().join("");

console.log(output);