题目描述
思路分析
个人觉得拆分成数组直接比较比较易于理解
代码解析
let input1 = '1.3.11-S2',
input2 = '1.3.11-S6',
res = [],
r = 0,
l = 0,
index = 0;
//需要注意的是reduce进行数组就链接自己
let a = input1.split(".").map(item => item.split("-")).reduce((a, b) => {
return a.concat(b)
});
let b = input2.split(".").map(item => item.split("-")).reduce((a, b) => {
return a.concat(b)
});
for (let i = 0; i < 4; i++) {
if (!func(a[i], b[i], i)) break;
}
//a input1的比较序号
//b input2的比较序号
//index 判断比较方式是比较大小还是比较字典序
function func (a, b, index) {
if (!!a && !!b) {
if (a == b) {
console.log('继续=>',);
return true
} else {
if (index = 3) console.log('不缺失,正常输出', a > b ? input1 : input2);
else console.log('不缺失,正常输出', parseInt(a) > parseInt(b) ? input1 : input2);
return false
}
} else {
if (!!a) console.log('[input2]缺失', input1);
else if (!!b) console.log('[input1]缺失', input2);
else console.log('双方缺失,输出前者', input1);
return false
}
}
后话
只是记录,正在找工作中,各位老板缺前端的可以私信,会vue,react,ts,js,22届本科毕业,一年工作经验