I'm trying to pop up a message to the user when one value is bigger than the other with javascript. I've used float but when the scripts should show true it doesn't show up
appVersionAndroid = '1.9'
appVersionWebAndroid = '1.10'
if(parseFloat(this.appVersionAndroid) < parseFloat(this.appVersionWebAndroid)) {
alert('needs update')
}
I'm trying to pop up a message to the user when one value is bigger than the other with javascript. I've used float but when the scripts should show true it doesn't show up
appVersionAndroid = '1.9'
appVersionWebAndroid = '1.10'
if(parseFloat(this.appVersionAndroid) < parseFloat(this.appVersionWebAndroid)) {
alert('needs update')
}
Share
Improve this question
edited Jun 20, 2018 at 9:37
coudy.one
1,43013 silver badges24 bronze badges
asked Jun 20, 2018 at 9:21
user6579134user6579134
8393 gold badges12 silver badges36 bronze badges
3
- Are you sure your condition is correct? – Ankit Agarwal Commented Jun 20, 2018 at 9:25
- This won't work. Instead, split the strings by dot and pare each number in the array, starting from left. – Farooq AR Commented Jun 20, 2018 at 9:26
-
appVersionAndroid
is1.9
which is the same as1.90
.appVersionWebAndroid
is1.10
.1.90 > 1.10
– Alex Commented Jun 20, 2018 at 9:41
3 Answers
Reset to default 4You're code is ok and there is no issue. There is a small mistake where you put the values. you put the appVersionAndroid
value "1.9" and appVersionWebAndroid
value 1.10, here in actually the first value is greater then second because float converter have two decimal number, I means 1.9 means 1.90 here which is greater then 1.10. So thats why your condition go to false.
Sorry for my english. But I think you understand what i want to say.
appVersionAndroid = "1.10"
appVersionWebAndroid = "1.90"
if(parseFloat(this.appVersionAndroid) < parseFloat(this.appVersionWebAndroid)){
alert('needs update')
}
1.9
is bigger than 1.10
. That's why alert doesn't show up.
the number 1.1 (which is the same as 1.10) is not greater that 1.9
You could for instance use 2 different variables for the version before and after the dots.
The main idea is to treat the number before and after the dot as two different integers numbers (for instance you can call them major and minor version), and it's not at all the same as having a big float number.
Then implement a parison function which would go like this :
First pare the major number. If it's equal, pare the minor number.