在JavaScript中不使用字符串方法且不使用数组对整数进行排序
我们需要编写一个带数字的JavaScript函数。该函数应返回可重新排列数字位数的最小数字。
例如-
如果输入号码是-
const num = 614532;
那么输出应该是-
const output = 123456;
唯一的条件是我们不能使用任何String方法或任何数组来存储数据。
示例
为此的代码将是-
const num = 614532;
const sortDigits = num => {
const getDigit = e => Math.floor(num / 10 ** e) % 10;
const l = Math.ceil(Math.log10(num)) − 1;
let e = l;
while (e−−) {
const left = getDigit(e + 1);
const right = getDigit(e);
if (left <= right){
continue;
};
num += (right − left) * 9 * 10 ** e;
e = l;
};
return num;
}
console.log(sortDigits(num));输出结果
控制台中的输出将是-
123456
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便