Skip to content

Instantly share code, notes, and snippets.

View GlistenSTAR's full-sized avatar
Fantastic speed

Glisten STAR GlistenSTAR

Fantastic speed
View GitHub Profile
function reverseInteger(input) {
let number = Math.abs(input); // holds a new copy of the absolute value of the input integer
let result = 0; // creates a result variable to hold the aggrigate of our while loop
while (number > 0) { // while the number is greater than 0 (simplified way of saying less than or equal to 1)
const lastDigit = number % 10; // each iteration runs modulo 10 on the number variable giving us the last digit
result = result * 10 + lastDigit; // sets result equal to itself multiplied by 10 plus the last digit
number = Math.floor(number / 10); // removes the last number for the next iteration
}