JavaScript Notes
Learn to build a prime number checker in JavaScript with trial division, Sieve of Eratosthenes, prime factorization, and range prime finder visualization.
What You'll Learn
- How to implement the trial division algorithm for primality testing
- Why we only need to check divisors up to √n (and why this is efficient)
- How to implement the Sieve of Eratosthenes to find all primes in a range
- How to display prime factorization of composite numbers
- How to visualize a grid of primes up to 100
Project Overview
A prime number checker tests whether an input number is prime, explains why (showing divisibility), and optionally generates all primes in a range. It's an excellent algorithm project that teaches loop optimization.
Component Structure
How It Works — Flow Diagram
Step-by-Step Build
Step 1 — HTML Structure
<div class="prime-checker">
<h1>Prime Number Checker</h1>
<div class="input-row">
<input type="number" id="numInput" placeholder="Enter a number (≥ 2)" min="2" step="1">
<button id="checkBtn">Check</button>
</div>
<div id="result" class="result hidden"></div>
<div id="factBox" class="fact-box hidden"></div>
<div class="sieve-section">
<h2>Primes up to 100</h2>
<div id="primeGrid" class="prime-grid"></div>
</div>
</div>Step 2 — isPrime (Trial Division)
function isPrime(n) {
n = Math.floor(n);
if (n < 2) return { prime: false, reason: "Numbers less than 2 are not prime" };
if (n === 2) return { prime: true, reason: "2 is the only even prime" };
if (n % 2 === 0) return { prime: false, reason: `${n} is even — divisible by 2` };
const limit = Math.sqrt(n);
for (let i = 3; i <= limit; i += 2) {
if (n % i === 0) {
return { prime: false, reason: `Divisible by ${i} (${n} ÷ ${i} = ${n / i})` };
}
}
return { prime: true, reason: `No divisors found between 2 and √${n} ≈ ${limit.toFixed(2)}` };
}Step 3 — Prime Factorization
Step 4 — Render Result
Step 5 — Sieve of Eratosthenes (Grid up to 100)
Step 6 — Highlight Checked Number in Grid
Step 7 — Wire Events & Initialize
Console Output
isPrime(2) → { prime: true, reason: "2 is the only even prime" }
isPrime(97) → checks 3,5,7,9 up to √97≈9.85 → no divisors → { prime: true }
isPrime(84) → 84 % 2 === 0 → { prime: false, reason: "Divisible by 2 (84 ÷ 2 = 42)" }
isPrime(1) → { prime: false, reason: "Numbers less than 2 are not prime" }
isPrime(49) → 49 % 7 === 0 → { prime: false, reason: "Divisible by 7 (49 ÷ 7 = 7)" }
factorize(84) → 2² × 3 × 7
factorize(360) → 2³ × 3² × 5Edge Cases to Handle
| Input | Expected |
|---|---|
| 1 | Not prime (by definition) |
| 2 | Prime (only even prime) |
| 0 or negative | Warning |
| Decimal (3.5) | Warning — must be integer |
| Very large number (1e9) | Trial division still fast up to ~1M; warn for huge numbers |
Challenge Yourself
- Twin primes — find all pairs of primes that differ by 2 (e.g., 11 & 13)
- Next prime — show the next prime after the entered number
- Goldbach's conjecture — express any even number > 2 as the sum of two primes
- Performance benchmark — time
isPrime(n)for n = 10⁶, 10⁷, 10⁸ - Miller-Rabin test — implement the probabilistic primality test for huge numbers
Best Practices
- Only loop up to √n — checking beyond is redundant
- Skip even numbers in the loop (
i += 2after checking 2 separately) — halves the work - Always validate input: check for
isNaN,< 2, and!Number.isInteger Number.isInteger(Number(raw))catches decimals like3.5- The Sieve is great for ranges — trial division is great for individual numbers
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Build a JavaScript Prime Number Checker - Step by Step Project Tutorial.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, mini, projects, prime
Related JavaScript Master Course Topics