bugger.
All challenges/JavaScriptmediumthe-search-that-starts-before-its-offset-v1
THE INVESTIGATION

The search that starts before its offset

A UTF-16 search returns a match before the byte offset where it was asked to start.

Real incidentsUnicode offsetsRegression testing

What’s going wrong

Input is an array of at most 100 {text, needle, offset} rows. Strings contain at most 40 BMP characters excluding surrogate code units; every character occupies two UTF-16LE bytes. offset is an integer in -200..200 measured in BYTES. Negative offsets count from the text byte length and clamp to zero; positive offsets clamp to the byte length. For a nonempty needle, return the first byte index at or after the normalized offset whose complete code-unit-aligned text equals needle, or -1. Search only positions aligned to the start of the text, never across code units. An empty needle returns the normalized offset exactly, even if odd. Return an array of numbers.

Expected behavior

Round an aligned search start without moving backward.

Reproduction

01 Odd byte start must not find the earlier match
[{"text":"aba","needle":"a","offset":1}]
[4]
02 Aligned start control
[{"text":"aba","needle":"a","offset":2}]
[4]
03 Zero start control
[{"text":"aba","needle":"a","offset":0}]
[0]

Before you begin

  • JavaScript strings and integer arithmetic
  • Byte offsets versus UTF-16 code units

Need a nudge?

Reveal one investigation hint at a time. Try each clue before opening the next.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.