Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
Javascript/Node.JS forEach loop with Count

I have a forEach loop which displays all records for 'communities'. For every 10th record, I want to insert an 'advert record', then continue with the loop of 'communities'.

With my existing code, although the 'advert record' is displayed at record 10, I've found that 'communities record 10' is being skipped/missed out, so it looks like this: R1 - R2 - R3 - R4 - R5 - R6 - R7 - R8 - R9 - ADVERT AT 10 - R11 - R12

Whereas what I want is this: R1 - R2 - R3 - R4 - R5 - R6 - R7 - R8 - R9 - ADVERT AT 10 - R10 - R11

Can someone please advise where my code is going wrong?

    <div class="index-record-grid-container" id="communityGroupsContainer">
        <% let count = 0; %>
        <% communities.forEach(function(community, i) { %>
            <% count = count+1; %>
            <% if (count === 10) { %>
                <div class="standard-record-container">
                    <div class="standard-ad-container">
                        <h3 class="standard-ad-header">Advertisement</h3>
                        <% promotions.slice(1,2).forEach(promotion => { %>
                            <img class="standard-ad-image lazy" data-src="https://res.cloudinary.com/dcjceb5aj/image/upload/w_400,c_fit,q_auto,f_auto,fl_lossy/<%- promotion.images[0].public_id %>"> 
                        <% }) %> 
                    </div>
                </div>
            <% } else if (count === 20) { %>
                <div class="standard-record-container">
                    <div class="standard-ad-container">
                        <h3 class="standard-ad-header">Advertisement</h3>
                        <% promotions.slice(1,2).forEach(promotion => { %>
                            <img class="standard-ad-image lazy" data-src="https://res.cloudinary.com/dcjceb5aj/image/upload/w_400,c_fit,q_auto,f_auto,fl_lossy/<%- promotion.images[0].public_id %>"> 
                        <% }) %> 
                    </div>
                </div>
            <% } else { %>
                <div class="standard-record-container">
                    <div class="record-grid-container">
                        <div class="box-record-username-row-2">
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!