Skip to main content
Code Review

Return to Question

added 41 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Problem Description:

Given a 32-bit signed integer, reverse digits of an integer in javascript.

Example 1:

  • Input: 123
  • Output: 321

Example 2:

  • Input: -123

  • Output: -321

Example 3:

  • Input: 120

  • Output: 21

Note: It should return zero when it reaches out of limit [−2 ^31, 2 ^31 − 1]

Given a 32-bit signed integer, reverse digits of an integer in JavaScript.

Example 1:

  • Input: 123
  • Output: 321

Example 2:

  • Input: -123

  • Output: -321

Example 3:

  • Input: 120

  • Output: 21

Note: It should return zero when it reaches out of limit [−2 ^31, 2 ^31 − 1]

My implementation

var reverse = function (x) {
 var minRange = Math.pow(-2, 31)
 var maxRange = Math.pow(2, 31) - 1
 var isNegative = false;
 if (x < 0) {
 isNegative = true;
 x = (x - x - x);
 }
 var result = Number(x.toString().split('').reverse().join(''));
 (isNegative) ? result = (result - result - result) : result;
 if (result < minRange || result > maxRange) {
 return 0
 } else {
 return result;
 }
};

Please help to improve.

Problem Description:

Given a 32-bit signed integer, reverse digits of an integer in javascript.

Example 1:

  • Input: 123
  • Output: 321

Example 2:

  • Input: -123

  • Output: -321

Example 3:

  • Input: 120

  • Output: 21

Note: It should return zero when it reaches out of limit [−2 ^31, 2 ^31 − 1]

My implementation

var reverse = function (x) {
 var minRange = Math.pow(-2, 31)
 var maxRange = Math.pow(2, 31) - 1
 var isNegative = false;
 if (x < 0) {
 isNegative = true;
 x = (x - x - x);
 }
 var result = Number(x.toString().split('').reverse().join(''));
 (isNegative) ? result = (result - result - result) : result;
 if (result < minRange || result > maxRange) {
 return 0
 } else {
 return result;
 }
};

Please help to improve

Problem Description:

Given a 32-bit signed integer, reverse digits of an integer in JavaScript.

Example 1:

  • Input: 123
  • Output: 321

Example 2:

  • Input: -123

  • Output: -321

Example 3:

  • Input: 120

  • Output: 21

Note: It should return zero when it reaches out of limit [−2 ^31, 2 ^31 − 1]

My implementation

var reverse = function (x) {
 var minRange = Math.pow(-2, 31)
 var maxRange = Math.pow(2, 31) - 1
 var isNegative = false;
 if (x < 0) {
 isNegative = true;
 x = (x - x - x);
 }
 var result = Number(x.toString().split('').reverse().join(''));
 (isNegative) ? result = (result - result - result) : result;
 if (result < minRange || result > maxRange) {
 return 0
 } else {
 return result;
 }
};

Please help to improve.

Source Link

Reverse Integer in JavaScript

Problem Description:

Given a 32-bit signed integer, reverse digits of an integer in javascript.

Example 1:

  • Input: 123
  • Output: 321

Example 2:

  • Input: -123

  • Output: -321

Example 3:

  • Input: 120

  • Output: 21

Note: It should return zero when it reaches out of limit [−2 ^31, 2 ^31 − 1]

My implementation

var reverse = function (x) {
 var minRange = Math.pow(-2, 31)
 var maxRange = Math.pow(2, 31) - 1
 var isNegative = false;
 if (x < 0) {
 isNegative = true;
 x = (x - x - x);
 }
 var result = Number(x.toString().split('').reverse().join(''));
 (isNegative) ? result = (result - result - result) : result;
 if (result < minRange || result > maxRange) {
 return 0
 } else {
 return result;
 }
};

Please help to improve

default

AltStyle によって変換されたページ (->オリジナル) /