Reverse numbers in function without using reverse() method in JavaScript


We are required to write a JavaScript function that takes in a number and returns its reversed number with converting it to an array or string.

Let's write the code for this function −

Example

const num = 234567;
const reverseNumber = (num, res = 0) => {
   if(num){
      return reverseNumber(Math.floor(num / 10), (res*10)+(num % 10));
   };
   return res;
};
console.log(reverseNumber(num));
console.log(reverseNumber(53536));
console.log(reverseNumber(76576));

Output

The output in the console will be −

765432
63535
67567

Updated on: 31-Aug-2020

990 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements