Sorted Search

Learn two different ways to complete the search of a sorted array. We'll go over the brute force method and a more elegant method.

Sorted Search

Instructions

Write a function that accepts a sorted array of integers and a number. Return the index of that number if present. The function should return -1 for target values not in the array.

Input: Array of Integers, Integer

Output: An integer from -1 onwards.

Examples:

search([1, 3, 6, 13, 17], 13); // -> 3
search([1, 3, 6, 13, 17], 12); // -> -1

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.