вариант
const isPrime = n => { //можно усовершенствовать
if (n === 2 || n === 3) return true
if (n <= 1 || n % 2 === 0 || n % 3 === 0) return false
const sqrt = Math.sqrt(n);
for (let i = 5; i <= sqrt; i += 2) if (n % i === 0) return false
return true
}
const arr = [1, 2, 3, 6, 9, 18, 13, 17]
//const res = arr.filter(isPrime)
const res = []
for (let i = 0; i < arr.length; i++) isPrime(arr[i]) && res.push(arr[i])
alert(res)
console.log(res)