For X of Y loop counters

Naim

I have this standings array

    { pos: 1, competitor_id: 583, rating: 2049, tier: 1 },
    { pos: 2, competitor_id: 862, rating: 1818, tier: 1 },
    { pos: 3, competitor_id: 67, rating: 1762, tier: 1 },
    { pos: 4, competitor_id: 758, rating: 1750, tier: 1 },
    { pos: 5, competitor_id: 713, rating: 1735, tier: 1 },
    { pos: 6, competitor_id: 868, rating: 1730, tier: 1 },
    { pos: 7, competitor_id: 946, rating: 1706, tier: 2 },
    { pos: 8, competitor_id: 893, rating: 1705, tier: 2 },
    { pos: 9, competitor_id: 731, rating: 1695, tier: 2 },
    { pos: 10, competitor_id: 869, rating: 1692, tier: 2 },
    { pos: 11, competitor_id: 998, rating: 1672, tier: 2 },
    { pos: 12, competitor_id: 968, rating: 1661, tier: 2 },
    { pos: 13, competitor_id: 460, rating: 1656, tier: 2 },
    { pos: 14, competitor_id: 879, rating: 1648, tier: 2 },
    { pos: 15, competitor_id: 969, rating: 1631, tier: 2 },
    { pos: 16, competitor_id: 925, rating: 1629, tier: 2 },
    { pos: 17, competitor_id: 999, rating: 1626, tier: 2 },
    { pos: 18, competitor_id: 991, rating: 1619, tier: 2 },
    { pos: 19, competitor_id: 956, rating: 1618, tier: 2 },
    { pos: 20, competitor_id: 711, rating: 1617, tier: 2 },
    { pos: 21, competitor_id: 964, rating: 1616, tier: 2 },

I want to increment tier by one every time we move 6 rows down. As you can see above it switches to tier 2 properly but fails to increment to 3 at pos:13 and to tier 4 at pos:19

I tried

const tiersize = 6;
// setting the counters
let tiercounter = 1;
let limit = tiersize;
// running the loop
for (const row of standings) {
  await connectionPromise
    .query({
      sql: 'update table set tier = ? where competitor_id = ?'
    }, [tiercounter, row.competitor_id], );
  if (row.pos == limit) {
    tiercounter += 1;
    limit += tiersize;
  }
}

What am I doing wrong?

Barmar

You're updating the database, but you're not updating the array as well. Add row.tier = tiercounter; to the loop.

const tiersize = 6;
// setting the counters
let tiercounter = 1;
let limit = tiersize;
// running the loop
for (const row of standings) {
  await connectionPromise
    .query({
      sql: 'update table set tier = ? where competitor_id = ?'
    }, [tiercounter, row.competitor_id], );
  row.tier = tiercounter;
  if (row.pos == limit) {
    tiercounter += 1;
    limit += tiersize;
  }
}

But rather than incrementing tiercounter as you go, you can simply calculate it from pos.

tiercounter = Math.floor((row.pos-1) / tiersize) + 1;

The updated code would then be:

const tiersize = 6;
// running the loop
for (const row of standings) {
  let tiercounter = Math.floor((row.pos - 1) / tiersize) + 1;
  await connectionPromise
    .query({
      sql: 'update table set tier = ? where competitor_id = ?'
    }, [tiercounter, row.competitor_id], );
  row.tier = tiercounter;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unable to reset counters in for loop

From Dev

(Python) multiple counters in a for loop?

From Java

How to make loop infinite with "x <= y && x >= y && x != y"?

From Dev

SELECT multiple columns with conditions from a table that contains only one VALUE column and two counters (X,Y)

From Dev

Pythonic way to default loop counters

From Dev

for x,y in function(): - Explain the python for loop structure

From Dev

r for loop for regression lm(y~x)

From Dev

R plot x,y points generated in loop

From Dev

For Each Loop - Text from color X to Y

From Dev

Creating for loop, df to ascii with x y z

From Dev

Python loop to divide x,y by two

From Dev

R loop to create data frames with 2 counters

From Dev

use variables to define counters in nested for loop

From Dev

For loop with multiple non-nested counters in scala

From Dev

Do While loop is not exiting its x<y loop despite x>y

From Dev

Why does `local x` inside a zsh for loop output `x=y`?

From

Go template: can't evaluate field X in type Y (X not part of Y but stuck in a {{range}} loop)

From Dev

for (x:y) loop isnt working properly in c++

From Java

Why this program with for loop give zero when y>5 and x=2

From Dev

Multiple threads in a for-loop using a parameterized function(x, y, z)

From Dev

How to store two variables (x.y) from a loop?

From Dev

Javascript for loop canvas drawing-go back to start x and y?

From Dev

python loop x + 1 times in a list of list until number y

From Dev

How to skip a for loop by X amount and continue for Y amount, then repeat

From Dev

Why doesn't Hotspot JIT perform loop unrolling for long counters?

From Dev

Can I use 2 counters in one while loop?

From Dev

applying for loop such that counters are multiplied rather than being added in python

From Dev

convert loop while with two counters into lambdas java 14

From Java

"x not in y" or "not x in y"

Related Related

  1. 1

    Unable to reset counters in for loop

  2. 2

    (Python) multiple counters in a for loop?

  3. 3

    How to make loop infinite with "x <= y && x >= y && x != y"?

  4. 4

    SELECT multiple columns with conditions from a table that contains only one VALUE column and two counters (X,Y)

  5. 5

    Pythonic way to default loop counters

  6. 6

    for x,y in function(): - Explain the python for loop structure

  7. 7

    r for loop for regression lm(y~x)

  8. 8

    R plot x,y points generated in loop

  9. 9

    For Each Loop - Text from color X to Y

  10. 10

    Creating for loop, df to ascii with x y z

  11. 11

    Python loop to divide x,y by two

  12. 12

    R loop to create data frames with 2 counters

  13. 13

    use variables to define counters in nested for loop

  14. 14

    For loop with multiple non-nested counters in scala

  15. 15

    Do While loop is not exiting its x<y loop despite x>y

  16. 16

    Why does `local x` inside a zsh for loop output `x=y`?

  17. 17

    Go template: can't evaluate field X in type Y (X not part of Y but stuck in a {{range}} loop)

  18. 18

    for (x:y) loop isnt working properly in c++

  19. 19

    Why this program with for loop give zero when y>5 and x=2

  20. 20

    Multiple threads in a for-loop using a parameterized function(x, y, z)

  21. 21

    How to store two variables (x.y) from a loop?

  22. 22

    Javascript for loop canvas drawing-go back to start x and y?

  23. 23

    python loop x + 1 times in a list of list until number y

  24. 24

    How to skip a for loop by X amount and continue for Y amount, then repeat

  25. 25

    Why doesn't Hotspot JIT perform loop unrolling for long counters?

  26. 26

    Can I use 2 counters in one while loop?

  27. 27

    applying for loop such that counters are multiplied rather than being added in python

  28. 28

    convert loop while with two counters into lambdas java 14

  29. 29

    "x not in y" or "not x in y"

HotTag

Archive