I’m trying to compare the value of some data from an ajax request (json) against a predefined number, and then output some HTML as a result.
Basically, if x is > than y, show ‘this’.
I’m saving the bits I need from the ajax request as:
var x = [];
var y = [];
var z = [];
I believe these are arrays (not sure if this has an impact when I get to my problem in a moment?)
Then I’d like to compare x, y and z against some preset numbers and output something as a result. Here’s what I’ve got so far…
if(x > '3') {
//do some stuff
}
else if(x > '2') {
//doing something else
}
else if(y > '5') {
//do this other thing
}
else if(z == '20') {
//do that
}
else {
//do the rest
}
This isn’t working because some of the values are double digit numbers, so I think I have to use parseInt, which I’ve tried, like this…
if(parseInt(x, 10) > '3') {
//do some stuff
}
else if(parseInt(x, 10) > '2') {
//doing something else
}
else if(parseInt(y, 10) > '5') {
//do this other thing
}
else if(parseInt(z, 10) == '20') {
//do that
}
else {
//do the rest
}
However, this isn’t working but it’s not giving me any errors either. I’m pretty sure a switch statement would be better too but I have a feeling it’s not that which is causing the problem.
I’m a designer struggling to get to grips with jquery so please excuse my naivety, any help greatly appreciated. Thanks in advance.
as you initially said, the problem is that you are comparing an array with a number, compare a array’s index which is numeric to a number..