Reverse an Integer - via JavaScript

Search for a command to run...

No comments yet. Be the first to comment.
This series will attempt to help to practice ds and also concepts using various examples and snippets.
Introduction This is the 2nd post in the DS and Algo practice series. Here we would have a look at what is a palindrome and implement it via JavaScript. What is a Palindrome ? Well, palindrome is a string which when reversed generates the same s...
Introduction This is the fourth post in the series DS & Also Practice - using examples & snippets. Problem Statement Given an input string, find the most recurring character. Ex - "aabbcccdddddd" -> here character d repeats 6 times, hence the most re...

Introduction This is the 2nd post in the DS and Algo practice series. Here we would have a look at what is a palindrome and implement it via JavaScript. What is a Palindrome ? Well, palindrome is a string which when reversed generates the same s...

Introduction The Data Structures and Algorithms Practice series starts with a very simple yet famous question, i.e. - Reverse a String. I would primarily be using JavaScript as a programming language for this series. Implementations via C#, Java wi...

React Js development series using modern functional components.

Input Integer -> Convert to String Array -> Search for negative sign -> Reverse the string -> Append the negative sign if the search yields true -> Convert the reversed string back to the integer.
function reverseInt(number) {
if (!number || isNaN(number)) {
return "Invalid format - input is not a integer";
}
let strArray = number.toString().split("");
let isNegative = strArray.indexOf("-") !== -1 ? true : false;
let reversedStr = strArray.reverse().join("");
reversedStr = isNegative ? `${"-"}${reversedStr}` : reversedStr;
return parseInt(reversedStr);
}
console.log(reverseInt(123));
console.log(reverseInt(-981));
console.log(reverseInt(500));
console.log(reverseInt(-900));
console.log(reverseInt(-900123));
console.log(reverseInt(""));
console.log(reverseInt("hello world"));
console.log(reverseInt(null));
console.log(reverseInt(NaN));

Math.sign(num) method returns +1/-1 depending upon the passed in number.
This method simplifies the above implementation.
Input Integer -> Convert to String Array and Reverse it -> Parse the reversed string back to Integer -> Multiply the reversed integer with Math.sign(Input Integer)
//Alternate approach using Math.sign() method
function reverseIntAlternate(number) {
if (!number || isNaN(number)) {
return "Invalid format - input is not a integer";
}
let reversedNumber = parseInt(number.toString().split("").reverse().join(""));
return reversedNumber * Math.sign(number);
}
console.log(reverseIntAlternate(123));
console.log(reverseIntAlternate(-981));
console.log(reverseIntAlternate(500));
console.log(reverseIntAlternate(-900));
console.log(reverseIntAlternate(-900123));
console.log(reverseIntAlternate(""));
console.log(reverseIntAlternate("hello world"));
console.log(reverseIntAlternate(null));
console.log(reverseIntAlternate(NaN));

Code is available at - https://github.com/anurag1302/dsalgo-javascript/blob/main/practice/reverse-int/index.js