The Target Search

EasyAcc. 97.4%
+15 XP 5

Index Discovery

Searching for an item's position is a core programming task.

The Assignment

Your function receives an array items and a target.

  1. Iterate through the array.
  2. If the current element equals the target, print its index and exit the loop immediately.
  3. If the loop finished and the target was never found, print -1.

01EXAMPLE 1

Inputitems=["a", "b", "c"], target="b"
Output1

Explanation: "b" is at index 1.

Constraints

  • Stop at the FIRST match.
  • Return -1 if missing.
ArraysLoopsSearching
JavaScript
Loading...
2 Hidden

Input Arguments

items["apple","banana"]
target"banana"

Expected Output

1

Click RUN to test your solution