10 XP challenge
The Closest Reading
easy
Build it yourself · 6 min
1 Learn2 Predict3 Solve
TaskYour mission
Return the index of the value closest to target; ties choose the earliest index. Empty input returns -1. All lists have at most 20 items; text is ASCII and at most 30 characters. Numeric inputs and list items are between -100 and 100 unless a narrower range is stated.
Examples
| Input | Return |
|---|---|
numbers = [], target = 4 | -1 |
numbers = [2,8], target = 5answer: -1 → 0. bestDistance: 201 → 3. distance: -3 → 3. The result is 0. | 0 |
Your code
Write your answer inside Solution, then use return to send it back.
Ln 1, Col 1
JavaScript
Try examples runs the public examples. Check my code checks your full answer and saves your completion.