daily pastebin goal
9%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Reverse a number.
  3. */
  4.  
  5. function reverseInt(num) {
  6.   //Convert the num to a string, reverse the chars.
  7.   const reversedStr = num.toString().split('').reverse().join('');
  8.  
  9.   //Convert back to an int, restore the sign (+ or -), return result as number.
  10.   return parseInt(reversedStr) * Math.sign(num);
  11. }
  12.  
  13. //Jest
  14. test('It should reverse 123 to 321.', () => {
  15.   expect(reverseInt(123)).toEqual(321);
  16. });
  17.  
  18. test('It should reverse 120 to 21.', () => {
  19.   expect(reverseInt(120)).toEqual(21);
  20. });
  21.  
  22. test('It should reverse -150 to -51.', () => {
  23.   expect(reverseInt(-150)).toEqual(-51);
  24. });
  25.  
  26. //Try it.
  27. console.log(reverseInt(25));
RAW Paste Data
Top