Chrillewoodz
Chrillewoodz

Reputation: 28368

Get row number from grid index

I got this grid with each square's index showing in the middle (0-63):

grid

Then I have a function which needs to return the row number when I pass it an index.

The row number can be between 0-7.

Function that should do this, with a poor attempt at retrieving the row number currently inside it:

function (index) {
  return Math.floor(index / 7);
}

Example outputs (index -> row output):

0 -> 0

5 -> 0

7 -> 0

8 -> 1

23 -> 2

35 -> 4

43 -> 5

63 -> 7

I looked at this question which I thought was about the same issue as I'm having but the answer there did not give the correct output so I must've misunderstood it.

How can I change my function to return the correct row number from a given index?

Upvotes: 0

Views: 1252

Answers (1)

Kurohige
Kurohige

Reputation: 1418

You need to divide by column count which are 8:

function (index) {
  return Math.floor(index / 8);
}

Upvotes: 8

Related Questions