thecodezone.
The Code Zone
← Back to the Developer Playbook

Code Zone Bug Lab / Episode 18

The Name That Doesn’t Match

Learn how arrays, objects, and a loop turn messy names into a roster match. Predict ten results, investigate a punctuation bug, then transfer the skill to a new problem.

DEV-161 · Arrays · objects · loops · cleaning input55–60 minutes with Exercise BNo setup or account required
Rob spots the trailing period that makes Milo. differ from Milo.
Rob spots the trailing period that makes Milo. differ from Milo.

Learn it → trace it → try it

One lookup, four small steps.

Imagine a space club that receives a name already extracted from a badge. A nickname, uppercase letters, or extra spaces should still lead to the right member. An unknown name should stay unknown.

Start here · What you will change

Make the lookup handle everyday variations.

The starter already handles uppercase letters, surrounding spaces, and approved nicknames. You have two additions to make in the JS tab:

  1. Improve the cleaning step: ignore periods (.), commas (,), exclamation marks (!), and question marks (?) at the end of the first word, before looking for that word in the aliases array.
  2. Update the roster: add one object for Rob, with the aliases rob, robert, bobby, and robbie. Keep the existing members and the reusable loop.

For example, "Milo." and "Milo?!" should match Milo. "Nova!" should match Nova, and "Pip?" should match Pip. A name outside the roster, such as "Samantha", must still return "" (no match).

Work on punctuation first, then add Rob. You are changing the cleanup and the roster data, not writing a separate condition for every input.

1. Store the choices

An array is a list. Each member is an object with two properties: name for the standard name and aliases for accepted spellings.

{ name: "Nova", aliases: ["nova", "novah"] }

2. Clean the input

trim() removes spaces at the ends. toLowerCase() makes letter case consistent. split(/\s+/)[0] takes the first word, even with repeated spaces.

" NOVAH Reed " becomes "novah". These three operations do not remove punctuation: "Milo." still becomes "milo.", which is different from the alias "milo".

3. Visit the list

for...of visits each member. includes(first) asks whether that member’s alias list contains the whole first word. It does not guess similar names.

for (const member of MEMBERS) {
  // Check this member's aliases.
}

4. Return a result

return member.name sends the match back and ends the function. A final return "" means no match after checking everyone.

The type check stops missing or non-text input before string methods run.

Why splitting the name is not enough.

Read the cleaning lines in order. Each step prepares the value for the next one:

  1. trim() removes spaces at the beginning and end: " Milo! Reed " becomes "Milo! Reed".
  2. toLowerCase() makes the letters lowercase: "milo! reed".
  3. split(/\s+/)[0] separates words at whitespace and keeps the first one: "milo!". The ! is still attached because it is not a space.
  4. Your new step: remove the allowed trailing punctuation to get "milo", then compare that value with each member’s aliases.

Find const cleaned and const first inside toMember. Put the punctuation cleanup before the loop’s includes(first) comparison.

See one way to clean the first word, explained
const cleaned = spoken.trim().toLowerCase();
const first = cleaned
  .split(/\s+/)[0]
  .replace(/[.,!?]+$/, "");

replace() returns text with a matching part replaced. Here, the matching part is punctuation at the end of the first word, and "" replaces it with nothing.

  • [.,!?] matches any one of the four listed punctuation marks.
  • + allows one or more marks, so "Milo?!" also works.
  • $ limits removal to the end of this first word.

The split and replace operations are chained: the first supplies the word, and the next cleans it. The final result is stored in first, so the existing includes(first) line can stay as it is.

This ticket only removes these four trailing marks. It does not remove letters, alter punctuation inside a name, or guess who an unknown person might be.

Trace one working example with Rob
  1. Input: " NOVAH Reed ".
  2. Clean it: "novah reed". First word: "novah".
  3. Milo’s aliases do not include it. Keep going.
  4. Nova’s aliases include it. Return "Nova".
  5. The function ends; Pip does not need checking.

Now explain what happens when no member matches. Why must the empty return come after the loop?

Known problems · Your ticket queue

Two reports. Two focused changes.

DEV-161A · A punctuation mark hides a match

Report: “MILO works, but Milo. comes back as unknown.” Reproduce both inputs. In toMember, update the first-word cleaning before the loop so it removes trailing periods (.), commas (,), exclamation marks (!), and question marks (?). Test a repeated ending such as Milo?! too.

Done when: Known names match with or without these trailing marks, and unknown names still return no match. Other special characters are outside this ticket.

DEV-161B · A new employee joins

Report: “Our fictional club hired Rob. He also goes by Robert, Bobby, or Robbie. None of those names are recognized yet.”

Your task: Add one member object with the standard name "Rob" and four lowercase aliases. Keep the lookup function reusable rather than adding a separate if statement for Rob.

Retest: "Rob", "ROBERT", " bobby ", "Robbie.", and "Robert from Star Club" should return "Rob". "Robin" and "Roberta" should stay unmatched. Milo, Nova, and Pip must still work.

Before the add-on: what changes when someone joins?

The array is the roster. Each object describes one employee; the aliases array lists approved names for that employee. The loop already checks every object, so adding one object lets it find a new person without rewriting the search.

// Example shape only — fill in your own values.
{ name: "Standard name", aliases: ["alias one", "alias two"] },

Keep commas between the objects. Store aliases in lowercase because the function lowercases the input before comparing it.

Need a starting point? Choose Load beginner TODOs above the editor. You can cancel without losing work. Its comments show where to investigate, not the completed answer. Choose Load explainer comments for a line-by-line explanation of the array, objects, string methods, conditions, loop, and return statements.

Exercise A · Predict, run, fix.

Allow 30 minutes. Use this page as your sandbox. Close AI assistants for this attempt; documentation is fine. Write predictions for the current starter, then run each case. Repair the punctuation case first, then complete the new employee add-on and retest. Check my fix covers both tickets; Rob-related checks stay red until you add his object. Your notes save in this browser and sync to your account when signed in. Run current code fills the preview input and logs its result without changing your code or writing your observations for you.

Ten inputs to investigate. Empty quotes mean an empty string; null is a non-text value.
InputYour predictionObserved resultTry it
"Milo."
"MILO"
"Miles from Star Club"
"Mylo"
" nova "
"Samantha"
""
"Pippin Reed"
" "
null

Choose a row to try it using your current editor code. Read the Console and record what you observed.

The null row’s Run current code button passes a real non-text value to the function. You can also try console.log(toMember(null)); in the JS tab. Add two tests of your own and explain your fix without reading the code aloud word for word.

Documentation: String.replace (new tab) ↗ · for...of (new tab) ↗

Your sprint ticket · In progress · DEV-161

A familiar name comes back as unknown.

Customer report: Milo is on our pretend Star Club roster, but a badge entered as Milo. returns no match.

Steps to reproduce

  1. Predict the ten results in Exercise A before running code.
  2. Try Milo. in the preview, then compare MILO and a known alias.
  3. Trace the JS function. Change how the first word is cleaned, then retest.

The input is an already extracted name, not a whole conversation. Only approved aliases count. Do not add fuzzy matching or accept everyone.

Acceptance criteria

  • Trailing punctuation is ignored: Milo., Milo, and Milo! all resolve to Milo.
  • Case and surrounding spaces do not affect a match.
  • Known aliases still work, including Miles from Star Club.
  • Unknown names, blank input, and non-text input return an empty string.
  • A partial name such as Milosh stays unmatched.
  • Add-on DEV-161B: Rob, Robert, Bobby, and Robbie resolve to Rob, including case, spaces, and trailing punctuation. Robin and Roberta stay unmatched.

Fictional learning demo. Use invented inputs only. No client systems or credentials are connected.

Reproduce → inspect → hypothesize → verify

Take the ticket.

Start with the two tasks and the cleaning walkthrough above. In the JS tab, update the first-word cleaning in toMember to remove trailing . , ! and ? before matching. Then add Rob and his four aliases to MEMBERS. The HTML and CSS are provided. Use Check challenge after you have recorded predictions and investigated. Checks include null and non-text inputs that cannot be entered directly in the text field.

Choose TODO guidance for where to investigate, or explainer comments for what each line does. Both leave the fixes for you. Loading a starter replaces edits only after you confirm. Reset keeps your selected mode.

Checking account and saved lab work…

Sign in to sync across devices

Loading the JavaScript and HTML editor…

Pass the checks or make three attempts to unlock an explained example.

Ready to test? Choose “Check challenge” after investigating.

    Open starter in a separate tab

    Code and prediction notes recover in this browser and sync when signed in. Instructor examples stay separate. Reset replaces code, not prediction notes.

    One possible solution

    Compare this approach with your own. Opening it does not change your code or mark the exercise complete.

    The alias list contains milo, but the starter compares it with milo. including the period. Remove trailing punctuation from the first word before the loop. Keep the aliases explicit and return an empty string for unknown inputs.

    Explain why cleaning belongs before includes, why return member.name stops the search, and why the final return belongs after the loop. Then complete Exercise B without copying the whole function.

    Try first. Reveal one clue at a time.

    Need a lead?

    1. Trace the value before the comparison.

    For Milo., what are cleaned and first? Compare that first word with the aliases in the array.

    2. Make one rule cover the variation.

    A period is a punctuation character. Remove trailing punctuation from the first word before checking aliases instead of adding every punctuation variant to the list.

    3. Read the documentation, then choose a small change.

    String.replace can remove matching characters. For this ticket, remove one or more trailing periods, commas, exclamation marks, or question marks. Keep the string check and the no-match return.

    Your lab report

    Show your reasoning.

    “I expected… I observed… The console showed… My hypothesis was… I changed… I verified…”

    Use Check challenge for automated behavior checks. Passing checks are useful evidence, not a complete code review.

    Give others room to investigate: label solutions as spoilers.

    Share your lab report on SKOOL (new tab) ↗

    Exercise B · 20 minutes

    Can you reuse the idea?

    Build a lookup for fictional observatory accounts. The data changes; the four steps stay useful.

    Start the account exercise →

    Finish with a five-minute explanation: What does the function receive? What does it return? What happens when nothing matches?