Skip to content

Instantly share code, notes, and snippets.

@GlistenSTAR
Created September 12, 2022 13:59
Show Gist options
  • Save GlistenSTAR/46e28f29bcc01e036bd039b91f77c862 to your computer and use it in GitHub Desktop.
Save GlistenSTAR/46e28f29bcc01e036bd039b91f77c862 to your computer and use it in GitHub Desktop.
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
}
const sign = input < 0 ? -1 : 1; // creates a variable either 1 or -1 depending on input numbers quality
return result * sign; // returns the result with the appropriate input quality (+ or -)
}
@GlistenSTAR
Copy link
Author

Add the code for reverse number.
For ex:) input number is 123 -> result is 321

@GlistenSTAR
Copy link
Author

Reverse_String is more simple.

const reversedString = str.split('').reverse().join('');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment