user1279988
user1279988

Reputation: 773

Please explain this line of js code.

I have the following javascript code block, and is not very clear about it:

var level = this.getLevelForResolution(this.map.getResolution());
var coef = 360 / Math.pow(2, level);

var x_num = this.topTileFromX < this.topTileToX ? Math.round((bounds.left - this.topTileFromX) / coef) : Math.round((this.topTileFromX - bounds.right) / coef);
var y_num = this.topTileFromY < this.topTileToY ? Math.round((bounds.bottom - this.topTileFromY) / coef) : Math.round((this.topTileFromY - bounds.top) / coef);

What does the < in this.topTileFromX < mean?

Upvotes: -1

Views: 58

Answers (3)

Hanky Panky
Hanky Panky

Reputation: 46900

That's a JavaScript Ternary operator. See Details Here

var x_num = this.topTileFromX < this.topTileToX ? Math.round((bounds.left - this.topTileFromX) / coef) : Math.round((this.topTileFromX - bounds.right) / coef);

is equivalent to the following expression

var x_num;

if (this.topTileFromX < this.topTileToX )
{
    x_num= Math.round((bounds.left - this.topTileFromX) / coef);
}
else
{
    x_num= Math.round((this.topTileFromX - bounds.right) / coef);
}

Upvotes: 1

Felipe Francisco
Felipe Francisco

Reputation: 1063

var x_num = this.topTileFromX < this.topTileToX ? Math.round((bounds.left - this.topTileFromX) / coef) : Math.round((this.topTileFromX - bounds.right) / coef);

It's a shorter if statement. It means:

var x_num;

if(this.topTileFromX < this.topTileToX)
{
   x_num = Math.round((bounds.left - this.topTileFromX) / coef);
}
else
{
   x_num = Math.round((this.topTileFromX - bounds.right) / coef);
}

Upvotes: 0

Albert Xing
Albert Xing

Reputation: 5788

< means "lesser than", like in Mathematics.

Thus

  • 2 < 3 returns true
  • 2 < 2 is false
  • 3 < 2 is also false

Upvotes: 0

Related Questions