# JavaScript JavaScript find first number in array that is <= to given number javascript find first in array,javascript find first item in

I have an array of prime numbers:

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

I want to find the first number in this list that is <= the number given.

For example ... `getHighestPrimeNumber(58)` ... should return 53, being the prime number with the greatest value which is also less than or equal to 58

Expect results:

My current approach is to iterate through the prime numbers but this is very inefficient, especially given that there may be 10,000+ numbers in the list - thanks

Vanilla JS or Lodash is fine

Since you posted this with `lodash` tag just FYI that this with it is trivial due to _.sortedIndex:

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

const closestPrime = (n) => {
let index = _.sortedIndex(primes, n)
return primes[index] == n ? primes[index] : primes[index-1]
}

console.log(closestPrime(58))
console.log(closestPrime(53))
console.log(closestPrime(52))``````
``<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>``

Seems like a field for a divide and conquer approach. Something like a Binary search:

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

function findHighestPrimeNumberRecursive(arr, ceiling) {
const midpoint = arr.length/2
if(arr[midpoint ] === ceiling){
// we found it!
return primes[arr.length-1];
} else {
if(arr[midpoint] <== ceiling) {
return findHighestPrimeNumberRecursive(arr.slice(0, midpoint), ceiling);
} else {
return findHighestPrimeNumberRecursive(arr.slice(midpoint, arr.length), ceiling);
}
}
}

}
``````

## Why is node.js/bluebird eating an exception?

Deep within my promise stack, I make this call: function isNameAvailable(name) { return registry.getName(name) .then(function(result) { return result ? false : true; })...

Deep within my promise stack, I make this call: function isNameAvailable(name) { return registry.getName(name) .then(function(result) { return result ? false : true; })...

1. node js bluebird
2. node js bluebird promise example
3. node js bluebird vs async
4. node js bluebird promise
5. node.js bluebird promise library
6. node js bluebird await
7. node js bluebird spread
8. node.js bluebird timeout
9. node js bluebird install
10. node js bluebird promise.each
11. node js bluebird module
12. node js bluebird finally
13. node.js use bluebird
14. node js promise vs bluebird
15. bluebird node js example
16. bluebird node js tutorial
17. bluebird node js package
18. node js mysql bluebird
19. node.js bluebird 사용법
20. node js require bluebird

## How to check browser is public-key credentials supported?

I am implementing webauthn using PHP, now I'm facing problem with how to detect browser is public-key credentials supported or not. If browser is supported public-key credentials then I have to start ...

I am implementing webauthn using PHP, now I'm facing problem with how to detect browser is public-key credentials supported or not. If browser is supported public-key credentials then I have to start ...

## How to access the class itself from within a class without using its name [duplicate]

Is it possible to access the class itself inside of a classes function: class MyClass { static get myFunction() { return "foo"; } constructor() { console.log(MyClass.myFunction); // "foo" -- ...

Is it possible to access the class itself inside of a classes function: class MyClass { static get myFunction() { return "foo"; } constructor() { console.log(MyClass.myFunction); // "foo" -- ...

1. python access class within itself

## Accessing objects

I wrote a simple function to access some values in the array below, and nested in the array are multiple objects. My question has to do with my 2 loops. I understand that I must initially loop ...

I wrote a simple function to access some values in the array below, and nested in the array are multiple objects. My question has to do with my 2 loops. I understand that I must initially loop ...

1. accessing objects in array javascript
2. accessing objects js
3. accessing objects via reference variables
4. accessing objects in java
5. accessing objects in python
6. accessing objects across appdomain boundaries
7. accessing objects in an arraylist java
8. accessing objects php
9. accessing objects from other classes java
10. accessing objects in arrays
11. accessing objects properties with bracket notation
12. accessing objects properties with variables
13. accessing objects in json
14. accessing objects in arraylist
15. accessing objects in json array
16. accessing objects in list python
17. accessing objects in typescript
18. accessing objects from other classes c#
19. accessing objects by index
20. accessing objects from multiple threads
1