I have the following code:
leaderboard.length
It is defined from the server and in my case it is 12
var l = leaderboard.length;
var i = 0;
for ( ; i < l; i++) {
var o = (i + 1) + ". " + name;
}
This will show me the position of the players from 1 to 11
But my problem is that even if leaderboard.length
it is 11, I want it to show all the positions, but at position 9 it does not show the number 9, that is, only:
1 - Player 1
2 - Player 2
3- Player 3
4- Player 4
5- Player 5
6- Player 6
7- Player 7
8- Player 8
- Player 9
10 - Player 10
11 - Player 11
You can use the ternary operator
A ? B : C
This operator evaluates
A
and, if istrue
, returnsB
. Otherwise, it returnsC
.Or you could use a
IF
Keep in mind that you are evaluating from 0, which means that the results will be in the range of 0~11 < 12, so the values from 1 to 12 will be displayed, I leave you the result in the following example:
the correct way would be: