How to Create a Mirror Inverse Program in JavaScript?

Estimated read time 1 min read

A mirror inverse program takes a number or string as input and returns its mirror inverse representation. Here’s how you could create such a program in JavaScript:

function mirrorInverse(input) {
  // Convert input to a string
  input = input.toString();

  // Reverse the string
  let reversedInput = input.split('').reverse().join('');

  // Convert the reversed string back to a number (if input was a number)
  return Number(reversedInput) || reversedInput;
}

// Test the function with a few examples
console.log(mirrorInverse(123)); // 321
console.log(mirrorInverse('abc')); // cba

In this example, the mirrorInverse function takes an input parameter and converts it to a string using toString(). The string is then reversed using split(''), reverse(), and join(''). Finally, the reversed string is converted back to a number using Number() (if input was originally a number). If the input is not a number, the reversed string is returned as is.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply