实现一个function 传入(v1,v2) 如果v1>v2 返回1 v1<v2返回0 其他返回0
compareVersion(‘1.1’, ‘1.1.0’), 0
compareVersion(‘13.37’, ‘1.2’), 1
compareVersion(‘0.1’, ‘1.1.1’). -1
function compareVersion(v1, v2) {
const v1Arr = v1.split(".")
const v2Arr = v2.split(".")
const maxLength = Math.max(v1Arr.length, v2Arr.length)
while (v1Arr.length < maxLength) {
v1Arr.push("0")
}
while (v2Arr.length < maxLength) {
v2Arr.push("0")
}
for (let i = 0; i < maxLength; i++) {
const num1 = parseInt(v1Arr[i])
const num2 = parseInt(v2Arr[i])
if (num1 > num2) {
return 1
} else if (num1 < num2) {
return -1
} else {
return 0
}
}
}
console.log(compareVersion('1.1', '1.1.0'), compareVersion('13.37', '1.2'), compareVersion('0.1', '1.1.1'))