Skip to main content

JavaScript Array Search — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Array Search lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Find a position

    easy10 min

    Show the index of Banana in a list of fruit.

  2. 2

    Check for a missing value

    easy10 min

    Use includes to check for a fruit that is not in the list.

  3. 3

    Report a missing index

    easy10 min

    Show the value indexOf gives back when nothing matches.

  4. 4

    Find the last match

    easy10 min

    Show the index of the last time a value appears.

  5. 5

    Search from a position

    medium10 min

    Find the next match after index 0 in a list with two matches.

Find the bug(5)

  1. 1

    Fix the truthy index check

    easy8 min

    An index of 0 is falsy, so a match at the front is reported as missing.

  2. 2

    Fix the wrong missing value

    easy8 min

    indexOf returns -1 when nothing matches, not undefined.

  3. 3

    Fix the type mismatch

    easy8 min

    The list holds numbers, but a string is being searched for.

  4. 4

    Fix first used instead of last

    easy8 min

    The last matching position is wanted, not the first.

  5. 5

    Fix the object compared by value

    easy8 min

    Two objects that look alike are still different objects.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace