2

This is more of a math problem really. I have a 3 column grid and am trying to determine which row a tap was in.

I just can't quite work out the math so that a tap of item 0,1,2 will output 0 and a tap of 3,4,6 will output a 1 etc. Any ideas for how to do this calculation?

Row0 = items 0, 1, 2
Row1 = items 3, 4, 5
Row2 = items 6, 7 ,8

Many thanks!

Fuego DeBassi
  • 2,967
  • 6
  • 29
  • 37

1 Answers1

3

You're just looking for division, not modulus or remainder:

var row = Math.floor(x / 3);

Example:

var i, row;
for (i = 0; i < 9; ++i) {
  row = Math.floor(i / 3);
  console.log(i + " => " + row);
}
.as-console-wrapper {
  max-height: 100% !important;
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875