用数字代码比较大小
-
需求:用数字代码比较大小
(如:132546 1026)
//设变量
let len;
let i = 0;
let subcodeVal1 = ('132546').split("")//将原数字数据转为数组
let subcodeVal2 = ('1026').split("")
//循环需要的长度
if (subcodeVal1.length > subcodeVal2.length) {
len = subcodeVal1.slice((0, subcodeVal2.length)).length //如果val1>val2取val2得长度比较
} else if (subcodeVal1.length == subcodeVal2.length) {
len = subcodeVal1.length
} else {
len = subcodeVal2.slice((0, subcodeVal1.length)).length
}
while (i < len) {
//两两对比;起大于止
if (subcodeVal1[i] > subcodeVal2[i]) {
console.log("val1>val2",subcodeVal1[i] , subcodeVal2[i])
break;//得出结果后,不再继续执行
}
i++;
}

浙公网安备 33010602011771号