我们需要编写一个 JavaScript 函数,它接受一个数字 num 作为第一个也是唯一的参数。
我们的函数应该找到并返回一个数字,它只包含输入数字的所有数字,并且只大于输入数字
如果不存在这样的数字,我们的函数应该返回 -1。
例如,如果函数的输入是 -
const num = 5656;
那么输出应该是 -
const output = 5665;
因为 5665 只包含 5656 的所有数字,并且刚好大于 5656。
以下是代码&mius;
const num = 5656; const justBigger = (num) => { const sorted = num => ('' + num).split('').sort((a, b) => b - a); const max = +sorted(num).join('') for (let i = num + 1; i <= max; i++) { if (max === +sorted(i).join('')){ return i; } }; return -1; } console.log(justBigger(num));输出结果
以下是控制台输出 -
5665