**Question:**

Given a number, find the largest prime numbers less than or equal to it.

**Answer:**

function isPrimeNumber(value) {
for (var i=2; i<=Math.ceil(Math.sqrt(value)); i++)
{
if (value % i === 0)
{
return false;
}
}
return true;
}
function findLargestPrimeNumberLessThan(value) {
if (isNaN(value)) {
throw "Not a number."
}
var input = Math.floor(value);
if (input <== 1) {
throw "number should be greater than or equal to 2";
}
for (var i=input; i>=2; i--) {
if (isPrimeNumber(i))
{
break;
}
}
return i;
}

### Like this:

Like Loading...

*Related*