思路:
1.利用split分割版本字符串为数组。eg:1.0.1 ==> [‘1’,‘0’,‘1’]
2.遍历两个数组,数组元素逐个转化为number,若数组越界,则定义为0
function compareVersion(version1,version2){
let arr1 = version1.split('.');
let arr2 = version2.split('.');
let maxLength = Math.max(arr1.length,arr2.length);
for(let i = 0; i<maxLength;i++){
let val1 = arr1[i]?Number(arr1[i]) : 0;
let val2 = arr2[i]?Number(arr2[i]) : 0;
if(val1>val2){
return 1
}else if(val1<val2){
return -1
}
}
//走到这一步说明两版本号相等
return 0
}