Adding sequential counter to filtered array elements in JavaScript loop

Hi everyone! I’m working on a JavaScript assignment and need some help with loop logic. My teacher wants me to create a program that filters out certain names from an array and adds sequential numbers to the remaining ones.

Here’s what I need to do:

  • Filter out any names that begin with the letter ‘B’
  • Add incrementing numbers (1, 2, 3, etc.) to the remaining names
  • Only use basic variables and arrays

My current array looks like this:

let students = ["Bob", "Maria", "John", "Patricia", "Ben", "Carlos", "Diana"];
let filterChar = "b";

The expected result should be:

// Expected Output
"1 => Maria"
"2 => John" 
"3 => Patricia"
"4 => Carlos"
"5 => Diana"

Here’s my attempt:

for (let i = 0; i < students.length; i++) {
    if (students[i][0] === filterChar.toUpperCase()) {
        continue;
    }
    console.log(`${i} => ${students[i]}`);
}

The issue is that my numbering doesn’t start from 1 and increment properly for the filtered results. How can I fix this?

You’re using the array index i for numbering, but that won’t work when you skip elements with continue. The index keeps going up while your numbering should stay sequential for the items you actually show. Add a separate counter for the filtered elements:

let students = ["Bob", "Maria", "John", "Patricia", "Ben", "Carlos", "Diana"];
let filterChar = "b";
let counter = 1;

for (let i = 0; i < students.length; i++) {
    if (students[i][0] === filterChar.toUpperCase()) {
        continue;
    }
    console.log(`${counter} => ${students[i]}`);
    counter++;
}

Use counter instead of i in your output, and only increment it when you actually process a name. This keeps your numbering clean and sequential no matter which array positions get filtered out.

yeah, so a lot of folks mess this up! you need to use a separate counter instead of the array index. just do let num = 1; and increment it when you actually log the names. it’ll fix that numbering issue for sure!

The problem happens when you use the loop index for numbering but skip elements with continue. You need a separate counter for displaying names. I’ve hit this same issue before - just increment a display counter only when you actually process a name:

let students = ["Bob", "Maria", "John", "Patricia", "Ben", "Carlos", "Diana"];
let filterChar = "b";
let displayNumber = 1;

for (let i = 0; i < students.length; i++) {
    if (students[i][0] === filterChar.toUpperCase()) {
        continue;
    }
    console.log(`${displayNumber} => ${students[i]}`);
    displayNumber++;
}

This way your numbering starts at 1 and increments correctly for each valid name that gets displayed.

Your logic’s right but you’re mixing up array position with display position. I made this exact mistake all the time when I first learned loops. Array indices and filtered sequence numbers are totally different things. Don’t use i directly in your console.log. Instead, track how many valid names you’ve actually processed: javascript let students = ["Bob", "Maria", "John", "Patricia", "Ben", "Carlos", "Diana"]; let filterChar = "b"; let sequenceNumber = 0; for (let i = 0; i < students.length; i++) { if (students[i][0] === filterChar.toUpperCase()) { continue; } sequenceNumber++; console.log(`${sequenceNumber} => ${students[i]}`); } See how I increment the sequence counter after checking the filter but before logging? Each displayed name gets the right sequential number no matter which array positions got skipped.

Had this exact same problem in bootcamp! The issue is that array indices don’t reset when you skip elements - they just keep counting up no matter what.

Here’s a much cleaner way to handle it:

let students = ["Bob", "Maria", "John", "Patricia", "Ben", "Carlos", "Diana"];
let filterChar = "b";
let position = 0;

for (let i = 0; i < students.length; i++) {
    if (students[i][0] !== filterChar.toUpperCase()) {
        position++;
        console.log(`${position} => ${students[i]}`);
    }
}

I flipped the condition to skip the continue statement entirely. Way more readable, and you only bump the position counter when you’re actually processing a valid name. The trick is realizing that your loop index and your display counter are totally separate things.

Everyone’s suggesting the manual counter approach, which works for school stuff. But when you hit real projects with data filtering, you’ll need something better.

I’ve dealt with this for years - writing loops manually gets annoying when data sources change or you need different filters. Instead of hardcoding everything, I use Latenode for automated workflows that filter arrays, add sequential numbers, and handle dynamic criteria.

Latenode lets you build a workflow that grabs your student array, applies whatever filter logic you want, and spits out numbered results automatically. If your teacher changes requirements later (filtering multiple letters, different formatting), just update the workflow instead of rewriting code.

The manual counter gets you through this assignment, but start thinking automation for real-world data processing. Check out https://latenode.com - these workflows handle data manipulation way cleaner than basic loops.