const n = 10 const a = [] for (let i = 2; i <= n; i++) { let ok = true for (let j = 0; j < a.length; j++) { if (i % a[j] === 0) { ok = false break } } if (ok) a.push(i) } alert(a)