Example of how to reverse number using JavaScript
A Pen by Vlad Bezden on CodePen.
Example of how to reverse number using JavaScript
A Pen by Vlad Bezden on CodePen.
| 'use strict'; | |
| describe('reverse', () => { | |
| const msg = 'should reverse 123 to 321', | |
| actual = reverse(123), | |
| expected = 321; | |
| it(msg, () => { | |
| expect(expected).toEqual(actual); | |
| }); | |
| }); | |
| function reverse(n) { | |
| let result = 0; | |
| while (n > 0) { | |
| result = result * 10 + n % 10; | |
| n = Math.floor(n / 10); | |
| } | |
| return result; | |
| } |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.min.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine-html.min.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/boot.min.js"></script> |
| <link href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.min.css" rel="stylesheet" /> |