1
var nextGreaterElement = function (n) {2
const MAX_VALUE = 2 ** 31 - 1;3
const nums = `${n}`.split("");6
for (let index = nums.length - 2; index >= 0; index--) {7
if (nums[index] < nums[index + 1]) {13
if (findPos === undefined) return -1;14
for (let index = nums.length - 1; index >= 0; index--) {15
if (nums[index] > nums[findPos]) {16
[nums[index], nums[findPos]] = [nums[findPos], nums[index]];23
.sort((a, b) => a - b)25
const result = Number(nums.slice(0, findPos + 1).join("") + mantissa);26
return result > MAX_VALUE ? -1 : result;