Integer Reversal

Integer Reversal

Given an Integer, return an integer that is the reverse ordering of numbers.

##For Example console.log(reverseInt(10)) => 1
console.log(reverseInt(24)) => 42

function reverseInt(n) {
	const reversed = n
		.toString()
		.split('')
		.reverse()
		.join('');

		if(n < 0 ) {
			return parseInt(reversed) * -1;
		}
		return parseInt(reversed);
}


We create a function reverseInt(n) parameter n.
We create a variable reversed and on that variable we chain couple of build in JavaScript methods on the parameter n
n.toString() This method toString() turns every value to string we need to turn to string so that after we can turn into an array.
After toString().split(‘’) we chain .split(‘’) without space between the quotes so the string will be converted into an array of single items.
toString().split(‘’).reverse() we chain .reverse() method to reverse the order of the items of the newly created array.
toString().split(‘’).reverse().join(‘’) in the end we join the reversed order of items into string with .join(‘’) method with quotes inside join.
But if the value of n is negative number for example -5 will be reversed to 5- which we don’t want that so we check with if condition n < 0 and if true we parseInt the reversed variable and multiply with -1 to get positive number.
In the end we return parsedInt again reversed variable

comments powered by Disqus