← Back to challenges

Algorithms: Binary Search

JavaScriptHardalgorithmsvalidationinterview

Instructions

Create a function that finds a target number in a list of prime numbers. Implement a binary search algorithm in your function. The target number will be from 2 through 97. If the target is prime then return "yes" else return "no".

Examples

var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

isPrime(primes, 3) ➞ "yes"

isPrime(primes, 4) ➞ "no"

isPrime(primes, 67) ➞ "yes"

isPrime(primes, 36) ➞ "no"

Notes

  • You could use built-in functions to solve this, but the point of this challenge is to see if you understand the binary search algorithm.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.