This is the 14th day of my participation in the August More Text Challenge
preface
When doing a logic problem, using return in forEach does not break out of the entire loop, only the current loop. Using break will result in an error. But using a return in a for loop breaks out of the loop.
Implement a method to compare two version numbers that meets the following requirements
Code implementation
function compareVersion(version1,version2){
var arr1=version1.split('. ')
var arr2=version2.split('. ')
var length,flag
if(arr1.length>arr2.length){
length=arr2.length
}else{
length=arr1.length
}
for(var index = 0; index < length; index++){
if(Number(arr1[index])>Number(arr2[index])){
console.log(1)
return 1
}else if(Number(arr1[index])<Number(arr2[index])){
console.log(-1)
return -1
}else if(length-1===index&&arr1.length===arr2.length){
console.log(0)
return 0
}else if(length-1===index&&arr1.length>arr2.length&&Number(arr1[arr1.length-1])! = =0) {console.log(1)
return 1
}else if(length-1===index&&arr1.length<arr2.length&&Number(arr1[arr1.length-1])! = =0) {console.log(-1)
return -1
}else if(length-1===index){
console.log(0)
return 0}}}/ / test
compareVersion('0.20.7'.'0.20.8')
compareVersion('0.20.9'.'0.20.8')
compareVersion('0.20.08'.'0.20.8')
compareVersion('0.21.08'.'0.20.8')
compareVersion('0.20.08'.'0.20.8.1')
compareVersion('0.20.8.0'.'0.20.8')
compareVersion('0.20.8.1'.'0.20.8')
compareVersion('0.020'.'0.20')
Copy the code
conclusion
This question mainly tests whether the logical thinking is complete, but there will also be some very basic but easy to ignore the question, write down to deepen the impression. The last three if judgments only consider the case of one more digit, which satisfies the question. In fact, if there are several more digits, you can use whether the extra digits add up to zero.
So the complete code looks like this:
function compareVersion(version1,version2){
var arr1=version1.split('. ')
var arr2=version2.split('. ')
var length,flag=0
if(arr1.length>arr2.length){
length=arr2.length
for(var i = length-1; i <arr1.length; i++){
flag+=i
}
}else{
length=arr1.length
for(var i = length-1; i <arr2.length; i++){
flag+=i
}
}
for(var index = 0; index < length; index++){
if(Number(arr1[index])>Number(arr2[index])){
console.log(1)
return 1
}else if(Number(arr1[index])<Number(arr2[index])){
console.log(-1)
return -1
}else if(length-1===index&&arr1.length===arr2.length){
console.log(0)
return 0
}else if(length-1===index&&arr1.length>arr2.length&&flag! = =0) {console.log(1)
return 1
}else if(length-1===index&&arr1.length<arr2.length&&flag! = =0) {console.log(-1)
return -1
}else if(length-1===index){
console.log(0)
return 0
}
}
}
compareVersion('0.20.7'.'0.20.8')
compareVersion('0.20.9'.'0.20.8')
compareVersion('0.20.08'.'0.20.8')
compareVersion('0.21.08'.'0.20.8')
compareVersion('0.20.08'.'0.20.8.1')
compareVersion('0.20.8.0'.'0.20.8')
compareVersion('0.20.8.1'.'0.20.8')
compareVersion('0.020'.'0.20')
compareVersion('0.020.1.0'.'0.20')
Copy the code