QWERTY
QWERTY

Reputation: 2315

JavaScript minus calculation between two variables

Basically I am trying to compare if the coordinates passing in match the items in an array list. So I have an array called busList with array item in this format:

27794.27939,43930.90485

And then I am trying to loop thru the array to compare the coordinates that I passed in. The coordinates that I passed in are two variables: coordx and coordy.

for(var i = 0; i < busList.length; i++){
    var parts = busList[i].split(",");
    buslocX = parts[0];
    buslocY = parts[1];

    if((coordx - buslocX < 0.0050) && (coordy - buslocY < 0.0050)){
        console.log(coordx - buslocX);
    }
}

If the coordx - the x coordinates in the array is less than 0.0050 and as well as the y, then I will perform something else. With these codes, I am trying to print the result of the minus, but I did not get anything in the console.

Any ideas? Thanks in advance.

In a short word, I am trying to check if coordx,coordy matches any items inside the array. But there will be slight difference like 0.0050 for the coordx,coordy with the array items. For example, I got a list of coordinates for array:

27794.27939,43930.90485
27539.43390,43422.26042

And the coordx I passed in is 27794.27920 and coordy is 43930.90480. Because both of the coordx and coordy is less than 0.0050 than first item in array, then I will perform something else.

Upvotes: 2

Views: 73

Answers (2)

Rasel
Rasel

Reputation: 5734

Try using

var parts = busList[i].split(".");

instead

var parts = busList[i].split(",");

Upvotes: 0

antyrat
antyrat

Reputation: 27765

Have you tried to convert buslocX and buslocY to float type?

buslocX = parseFloat(parts[0]);
buslocY = parseFloat(parts[1]);

split method converts string to array of strings and in that case bahaviour of calculations may be unexpected.

Upvotes: 2

Related Questions