Home > Software engineering >  How do I iterate through an inline var using pug
How do I iterate through an inline var using pug

Time:05-17

I am attempting to iterate through an array and each time add 1 to the inline pug variable. So far this is what I got.

ul()
            - var i = -1
            each card in cards
                -var L = i   1
                li
                    a.btn(href="/addmonster"   L)
                        div=card.name
                        div= "Attack: " card.attack
                        div= "HP: " card.hp
                        div= "Attribute: " card.attribute
                        div= "Energy: " card.energy

When I run the page "L" is always = to 0. I want it to add 1 each time. The question really comes down to what am I doing wrong?

CodePudding user response:

As said in the comments you are not incrementing the i, you can fix your issue like this:

ul.benchcards
  - var i = -1;
  each card in cards
    li
      a.btn(href='/addmonster'   i  )
        div= card.name
        div= 'Attack: '   card.attack
        div= 'HP: '   card.hp
        div= 'Attribute: '   card.attribute
        div= 'Energy: '   card.energy

CodePudding user response:

Sorry, the calculation should be i=i 1 NOT var i = i 1, this resets i.

ul()
            - var i = -1
            each card in cards
                i = i   1
                li
                    a.btn(href="/addmonster"   #{i})
  • Related