-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathreverseInteger.js
More file actions
41 lines (29 loc) · 821 Bytes
/
reverseInteger.js
File metadata and controls
41 lines (29 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Write a function `reverseInteger` which takes an integer as input and returns the integer with its digits reversed. If the input is negative, the reversed integer should also be negative.
What is reversing an integer?
- Reversing an integer means rearranging its digits in the opposite order while maintaining its sign.
Example:
- Input: 123
- Output: 321
- Input: -456
- Output: -654
- Input: 100
- Output: 1
- Input: 0
- Output: 0
Once you've implemented the logic, test your code by running
- `npm run test-reverseInteger`
*/
function reverseInteger(num) {
let s = String(num);
let sign=1;
if(s[0]=='-'){
sign=-1;
s=s.slice(1);
}
let ns=s.split("").reverse().join("");
let ans=Number(ns);
ans*=sign;
return ans;
}
module.exports = reverseInteger;