-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse-integer.js
More file actions
57 lines (45 loc) · 1.37 KB
/
reverse-integer.js
File metadata and controls
57 lines (45 loc) · 1.37 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Given a 32-bit signed integer, reverse digits of an integer.
//Example 1:
//Input: 123
//Output: 321
//Example 2:
//Input: -123
//Output: -321
//Example 3:
//Input: 120
//Output: 21
//Note:
//Assume we are dealing with an environment which could only hold integers within the 32-bit signed
//integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
//Your runtime beats 51.87 % of javascript submissions.
/**
* @param {number} x
* @return {number}
*/
// console.log(reverseNums(123), '123', 321);
// console.log(reverseNums(-123), '-123', -321);
// console.log(reverseNums(120), '120', 21);
// console.log(reverseNums(121235512350), '121235512350', 0);
function reverseArray(inputArray) {
return inputArray.reverse();
}
function reverse (inputNum) {
var arrayNum = inputNum.toString().split("");
var returnString = '';
var lowerLimit = -2147483648;
var upperLimit = 2147483647;
if (arrayNum[0] === '-') {
arrayNum.splice(0,1);
reverseArray(arrayNum);
arrayNum.splice(0,0,'-');
returnString = arrayNum.join('');
} else {
reverseArray(arrayNum);
returnString = arrayNum.join('');
}
if (returnString*1 < lowerLimit || returnString*1 > upperLimit) {
return returnString*0;
} else {
return returnString*1;
}
};