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.
- Iterate through the array.
- If the current element equals the
target, print its index and exit the loop immediately. - If the loop finished and the target was never found, print -1.
01EXAMPLE 1
Input
items=["a", "b", "c"], target="b"Output
1Explanation: "b" is at index 1.
Constraints
- Stop at the FIRST match.
- Return -1 if missing.
ArraysLoopsSearching
JavaScriptSystem handles I/O — write your function only
Loading...
2 Hidden
Input Arguments
items["apple","banana"]
target"banana"
Expected Output
1
Click RUN to test your solution