# Cycle problem for JavaScript

4

I have the following code:

`leaderboard.length` 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 although% co_of% is 11, I want it to show all the positions, but to position 9 it does not show you the number 9 or just:

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

asked by Eduardo Sebastian 19.04.2017 в 16:27
source

3

Or you could use a `IF`

``````var l = leaderboard.length;
var i = 0;

for ( ; i < l; i++) {

if(i==8){
var o = ". " + name;
}else{
var o = (i + 1) + ". " + name;
}
``````

source
5

You can use the ternary operator `A ? B : C`

``````l = leaderboard.length;
i = 0;

for( ; i < l; i++ ) {

o = ( i == 8 ) ? ( name ) : ( (i + 1) + ". " + name );
}
``````

Said operator evaluates `A` and, if `true` , returns `B` . Otherwise, it returns `C` .

1

Keep in mind that you are evaluating from 0 what 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 the result in the following example:

``````var l = 12;//leaderboard.length;
var i = 0;

for ( ; i < l; i++) {
var o = (i + 1) + ". " + name;
console.log(o);
}``````

The correct thing would be:

``````var leaderboard="123456789012";