Part 1 · Lesson 4 · 22 min read

The for Loop

The most important idea in this series. Start, condition and update, traced step by step with a dry-run table until the pattern is clear.

On this page

  1. Why loops exist
  2. The three parts of a for loop
  3. The exact order things happen
  4. Traced: printing 1 to 3
  5. How many times will it run?
  6. Counting down, skipping, starting at 0
  7. The accumulator: adding things up
  8. Traced: the sum of 1 to 4
  9. break and continue
  10. Off-by-one and other loop bugs
  11. A recipe for writing any loop
  12. Practice questions (10)
  13. Make it stick

Why loops exist

Print the numbers from 1 to 5. With what you know so far:

JavaScript
console.log(1);console.log(2);console.log(3);console.log(4);console.log(5);// ...and for 1 to 1000? 1000 lines. There has to be a better way.

A loop is a statement that repeats a block of code again and again, and stops when a condition is no longer true. Each time the block runs is called an iteration. Here is the same output in three lines:

JavaScript
for (let i = 1; i <= 5; i++) {  console.log(i);}/* Output:12345*/

Change 5 to 1000 and it prints a thousand lines, but the code is still three lines long. Nearly every DSA solution has at least one loop. So this lesson is the base for everything after it. Take your time and trace every example.

The three parts of a for loop

A for loop is a loop that keeps all its counting in one place. Inside the round brackets it has three parts, separated by semicolons: start, check and update. After the brackets comes the body (the code to repeat) in curly braces. The table lists all four steps in the order you read them in the code.

JavaScript
for (let i = 1;   i <= 5;   i++) {//   ─────────   ──────   ───//   1. START    2. CHECK    4. UPDATE//   runs once   before      after every//   at the      every       iteration//   beginning   iteration  console.log(i);   // 3. BODY — runs only while CHECK is true}
PartHereJob
1. Startlet i = 1Create the loop variable. Runs once.
2. Checki <= 5Asked before every iteration. If true, run the body. If false, leave the loop.
3. Bodyconsole.log(i)The work to repeat. Can use i.
4. Updatei++Runs after every iteration. It moves i closer to the stopping point.

The variable is usually called i (short for “index”). It is the loop variable: the counter that changes in every iteration and decides when the loop stops.

The exact order things happen

This is the most important part to remember. For for (start; check; update) { body }, the order is:

The order of execution

start → check → body → update → check → body → update → check → … → check (false) → exit

  • Start happens once, before anything else.
  • Check happens one more time than the body. The last check fails, and that failing check ends the loop.
  • Update always comes after the body, never before.

Traced: printing 1 to 3

Press Next, and say each step out loud before you see it. Watch the order of check, body and update:

step 1 / 12
Set up
START: let i = 1

Runs exactly once. The loop variable i is created with value 1.

▶for (let i = 1; i <= 3; i++) {2  console.log(i);3}4console.log("done");
Variables
i1
Console
(nothing printed yet)
Yellow is a check, blue is the body printing, violet is the update. The loop ends on a check, when i is 4. · interactive
Dry run · for (let i = 1; i <= 3; i++)
Iterationi at checki <= 3?Printsi after update
11true12
22true23
33true34
—4false(loop ends)—

Three iterations of the body, four checks. After the loop, i is 4, which is one more than the last value printed.

How many times will it run?

You will be asked this often. It is also how you will later work out Big-O (Lesson 12), a way to describe how the running time of code grows with the input size. Three forms cover almost everything:

Loopi takes the valuesRuns
for (let i = 1; i <= n; i++)1, 2, …, nn times
for (let i = 0; i < n; i++)0, 1, …, n − 1n times
for (let i = a; i <= b; i++)a, a + 1, …, bb − a + 1 times

The second form (start at 0, use <) may look strange now. But it is the most common form in DSA, because arrays (lists of values) and strings number their items from 0 (Lesson 8).

Counting down, skipping, starting at 0

Change any of the three parts to change the pattern of values:

JavaScript
// Count down: start high, check >=, update with --for (let i = 5; i >= 1; i--) console.log(i);   // 5 4 3 2 1 (one per line)// Every second number: update with += 2for (let i = 2; i <= 10; i += 2) console.log(i); // 2 4 6 8 10// Run exactly n times, counting from 0 — the most common form in DSAconst n = 3;for (let i = 0; i < n; i++) console.log("hello"); // hello ×3 (i is 0, 1, 2)
Short bodies

When the body is one statement, you may leave out the braces, as above. While you are learning, keep the braces. Then you will not make a common mistake when you add a second line later (the second line would not be part of the loop).

The accumulator: adding things up

Printing is useful, but most loop problems ask for one answer built from many steps, such as a total, a count or a product. The pattern always has the same three moves:

  1. Before the loop, create a variable for the answer and give it a starting value. Use 0 for a sum or a count. Use 1 for a product.
  2. Inside the loop, update it using the value of this iteration.
  3. After the loop, the variable holds the answer.

That variable is called an accumulator (a variable that collects a result step by step, like a jar where you keep adding coins). It must be declared outside the loop. If you declare it inside, it is reset to 0 in every iteration.

Traced: the sum of 1 to 4

step 1 / 16
Set up
let sum = 0

The accumulator starts empty. 0 means “nothing added yet”. It lives OUTSIDE the loop so it keeps its value between iterations.

▶let sum = 0;2for (let i = 1; i <= 4; i++) {3  sum = sum + i;4}5console.log(sum);
Variables
sum0
Console
(nothing printed yet)
Watch sum grow: 0 → 1 → 3 → 6 → 10. Each iteration adds the current i. · interactive
Dry run · sum of 1 to 4
isum beforesum = sum + isum after
100 + 11
211 + 23
333 + 36
466 + 410
5—check fails, loop ends10

break and continue

Two keywords change this normal order. break leaves the loop right now. Use it when you have found what you were looking for. continue skips the rest of this iteration and jumps to the update.

JavaScript
// break: leave the loop immediatelyfor (let i = 1; i <= 10; i++) {  if (i === 4) break;  console.log(i);          // 1 2 3, then the loop stops at 4}// continue: skip the rest of THIS iteration, go to the updatefor (let i = 1; i <= 5; i++) {  if (i % 2 === 0) continue;  console.log(i);          // 1 3 5 — even numbers are skipped}/* Output:123135*/

Off-by-one and other loop bugs

BugLooks likeWhat happens
Off-by-onei < 5 when you meant i <= 5The loop runs one time too few (or one time too many). This is the most common loop bug. Always check the first and the last value of i.
Accumulator inside the loopfor (…) { let sum = 0; sum += i; }sum is reset in every iteration, so the answer is just the last i.
Update goes the wrong wayfor (let i = 1; i <= 5; i--)i never gets above 5, so the check is always true. This is an infinite loop (a loop that never stops). Press Ctrl+C to stop the program.
Wrong starting valueProduct starting at 0Anything times 0 is 0. A product must start at 1.

A recipe for writing any loop

  1. What values should i take? Write the first and the last one. They give you the start and the check.
  2. How does i move? By +1, −1, +2 and so on. This is the update.
  3. What happens in each iteration? This is the body.
  4. Do I need one answer at the end? If yes, add an accumulator before the loop.
  5. Dry-run the first two iterations and the last one. Most bugs show up there.

Practice questions

Use the recipe for each question. Write the dry-run table for at least the first three before you open the answers.

Q1Print 1 to NEasy

Print every number from 1 to N, one per line.

Example 1
Input: N = 5
Output:
1
2
3
4
5
Explanation: Every whole number from 1 up to and including 5, one per line.
Example 2
Input: N = 1
Output: 1
Explanation: The smallest case: the loop runs exactly once.
Stuck? Open one hint at a time
Hint 1
Which value should i start at, and which value is the last one printed?
Hint 2
Start at 1, keep going while i <= N, and add 1 each time.
Show all 3 solutions with explanations
Approach 1for loop
The idea

First value 1, last value N, step +1, and the body prints i.

JavaScript
const N = 5;for (let i = 1; i <= N; i++) {  console.log(i);}/* Output:12345*/
How it works

The loop runs N times: i takes the values 1, 2, …, N, and the check fails when i becomes N + 1.

Approach 2while loop
The idea

A while loop repeats as long as its condition is true. It has the same three parts as for, but spread out: start before the loop, check in the brackets, update at the end of the body.

JavaScript
const N = 5;let i = 1;while (i <= N) {  console.log(i);  i++;}/* Output:12345*/
How it works

Forgetting i++ is the most common mistake here. Then i stays 1 forever, and the loop never ends. Lesson 5 covers while in detail.

Approach 3Build one string, print once
The idea

Collect all the numbers into one string (a piece of text) and print it once at the end.

JavaScript
const N = 5;let out = "";for (let i = 1; i <= N; i++) {  out += i + (i < N ? " " : "");   // a space after every number except the last}console.log(out); // 1 2 3 4 5
How it works

This is useful when a problem asks for the numbers on one line. The i < N ? " " : "" part (a ternary, from Lesson 3) avoids a space after the last number.

Which approach should you use?

Use the for loop (Approach 1). You know the exact range, and that is what for is made for.

Q2Print N down to 1Easy

Print the numbers from N down to 1.

Example 1
Input: N = 4
Output:
4
3
2
1
Explanation: Start at N and go down by one until 1.
Stuck? Open one hint at a time
Hint 1
Reverse all three parts of the loop from question 1.
Hint 2
Start at N, keep going while i >= 1, and subtract 1 each time with i--.
Show all 2 solutions with explanations
Approach 1Count down
The idea

Start at N, check i >= 1, update with i--.

JavaScript
const N = 4;for (let i = N; i >= 1; i--) {  console.log(i);}/* Output:4321*/
How it works

A common mistake is keeping i <= N as the check. As i gets smaller, that stays true forever, so the loop never stops (an infinite loop).

Approach 2Count up, print N − i + 1
The idea

Keep the normal loop from 1 to N, but print a value that you work out from i.

JavaScript
const N = 4;for (let i = 1; i <= N; i++) {  console.log(N - i + 1);}/* Output:4321*/
How it works

When i is 1, the value is N. When i is N, the value is 1. Printing a formula that uses the loop variable is a technique you will use often in pattern problems (Lesson 6).

Which approach should you use?

Approach 1 is clearer. Approach 2 is worth knowing. It shows that the value you print does not have to be the loop variable itself.

Q3Even numbers up to NEasy

Print all even numbers from 1 to N.

Example 1
Input: N = 10
Output:
2
4
6
8
10
Explanation: The even numbers between 1 and 10.
Example 2
Input: N = 7
Output:
2
4
6
Explanation: 8 is larger than 7, so the last even number is 6.
Stuck? Open one hint at a time
Hint 1
One way: visit every number and keep only the even ones. How do you test for even?
Hint 2
A faster way: start at 2 and add 2 each time.
Show all 3 solutions with explanations
Approach 1Visit every number, keep the even ones
The idea

Loop from 1 to N and print i only when i % 2 === 0.

JavaScript
const N = 7;for (let i = 1; i <= N; i++) {  if (i % 2 === 0) console.log(i);}/* Output:246*/
How it works

This is a loop combined with an if from Lesson 3. The loop runs N times, but only about half of the numbers are printed.

Approach 2Jump from even to even
The idea

Start at 2 and add 2 each time, so i is always even.

JavaScript
const N = 7;for (let i = 2; i <= N; i += 2) {  console.log(i);}/* Output:246*/
How it works

The loop runs only about N / 2 times and needs no if. Changing the update step is often the simplest way to skip values.

Approach 3Count the k-th even number
The idea

The k-th even number is 2 × k. Loop k from 1 while 2k ≤ N.

JavaScript
const N = 7;for (let k = 1; 2 * k <= N; k++) {  console.log(2 * k);}/* Output:246*/
How it works

This does the same amount of work as Approach 2, but it is written as a formula. Thinking “the k-th item is …” helps in many problems about sequences (lists of numbers that follow a rule).

Which approach should you use?

Approach 2 is the best. It does the least work, and its purpose is clear. Saying that it halves the number of iterations shows that you think about efficiency.

Q4Sum of 1 to NEasy

Find 1 + 2 + … + N.

Example 1
Input: N = 5
Output: 15
Explanation: 1 + 2 + 3 + 4 + 5 = 15.
Example 2
Input: N = 100
Output: 5050
Explanation: Adding the numbers from 1 to 100 gives 5050.
Stuck? Open one hint at a time
Hint 1
You need one answer built from many steps. Create a variable for the total before the loop.
Hint 2
Start the total at 0 and add i on every iteration.
Show all 2 solutions with explanations
Approach 1Loop with an accumulator
The idea
  1. Before the loop: sum = 0.
  2. Inside the loop: sum += i.
  3. After the loop: sum is the answer.
JavaScript
function sumTo(N) {  let sum = 0;  for (let i = 1; i <= N; i++) {    sum += i;  }  return sum;}console.log(sumTo(5));   // 15console.log(sumTo(100)); // 5050
How it works

The loop is wrapped in a function (a named block of code that you can run many times with different inputs; Lesson 7 teaches it) so we can test it with two inputs. For now, read sumTo(5) as “run this with N = 5”. The word return sends the answer back to the caller.

Dry run · sumTo(5)
isum after
11
23
36
410
515
Approach 2The formula N × (N + 1) / 2
The idea

Pair the first and last numbers: 1 + 100, 2 + 99, and so on. Each pair adds up to N + 1, and there are N / 2 pairs.

JavaScript
function sumTo(N) {  return (N * (N + 1)) / 2;}console.log(sumTo(5));   // 15console.log(sumTo(100)); // 5050
How it works

There is no loop at all. The answer takes the same time for N = 5 or N = 5,000,000. Lesson 12 calls this O(1), which means constant time.

Which approach should you use?

Know both. Write the loop if the question is about practising loops, and mention the formula afterwards. Interviewers like candidates who notice when a loop is not needed.

Q5FactorialEasy

Find n! = n × (n − 1) × … × 1, for a whole number n ≥ 0.

Example 1
Input: 5
Output: 120
Explanation: 5! = 5 × 4 × 3 × 2 × 1 = 120.
Example 2
Input: 0
Output: 1
Explanation: By definition, 0! = 1. (n! means n factorial: n multiplied by every whole number below it, down to 1.)
Stuck? Open one hint at a time
Hint 1
This is an accumulator (see the accumulator section above), but for multiplication.
Hint 2
What must a product start at? (Anything multiplied by 0 is 0.)
Show all 3 solutions with explanations
Approach 1Count up from 2
The idea

Start with product = 1 and multiply by 2, 3, …, n.

JavaScript
function factorial(n) {  let product = 1;              // not 0 — anything × 0 is 0  for (let i = 2; i <= n; i++) {    product *= i;  }  return product;}console.log(factorial(5)); // 120console.log(factorial(0)); // 1
How it works
Dry run · factorial(5)
iproduct beforeproduct after
212
326
4624
524120

For n = 0 or 1 the loop never runs, so the answer stays 1. This is correct without a special case.

Approach 2Count down from n
The idea

Multiply n × (n − 1) × … × 2, in the same order as the definition.

JavaScript
function factorial(n) {  let product = 1;  for (let i = n; i >= 2; i--) {    product *= i;  }  return product;}console.log(factorial(5)); // 120
How it works

Multiplication gives the same result in any order, so counting down works too.

Approach 3while loop
The idea

Multiply by n, then reduce n by 1, until n reaches 1.

JavaScript
function factorial(n) {  let product = 1;  while (n > 1) {    product *= n;    n--;  }  return product;}console.log(factorial(5)); // 120
How it works

The parameter n (the name for the input of a function) acts as the counter itself. Changing a number parameter inside a function does not change the value in the code that called it.

Which approach should you use?

Any of the three is correct, and Approach 1 is the most common. Factorials grow very fast. From 23! onwards, a JavaScript number cannot store the exact value. So interview questions often ask for the answer modulo a number (the remainder after dividing by it; Lesson 13).

Q6Multiplication tableEasy

Print the multiplication table of n, from × 1 to × 10.

Example 1
Input: 7
Output:
7 x 1 = 7
7 x 2 = 14
...
7 x 10 = 70
Explanation: Ten lines: 7 multiplied by each number from 1 to 10.
Stuck? Open one hint at a time
Hint 1
The loop variable does not have to be the thing you print. Here it is the multiplier.
Hint 2
Loop i from 1 to 10 and print n, i and n * i.
Show all 2 solutions with explanations
Approach 1Join the text with +
The idea

Loop i from 1 to 10 and build each line from pieces.

JavaScript
const n = 7;for (let i = 1; i <= 10; i++) {  console.log(n + " x " + i + " = " + n * i);}/* Output:7 x 1 = 77 x 2 = 147 x 3 = 217 x 4 = 287 x 5 = 357 x 6 = 427 x 7 = 497 x 8 = 567 x 9 = 637 x 10 = 70*/
How it works

n * i is worked out before the joining, because * has a higher priority than +.

Approach 2Template literal
The idea

Write the line as text between backticks, with ${ } placeholders for the values.

JavaScript
const n = 7;for (let i = 1; i <= 3; i++) {  console.log(`${n} x ${i} = ${n * i}`);}/* Output:7 x 1 = 77 x 2 = 147 x 3 = 21*/
How it works

This is easier to read than many + signs. There is also no risk of joining when you meant to add. (The example stops at 3 to keep the output short.)

Which approach should you use?

Both print the same text. Template literals (Approach 2) are the newer way and are easier to read.

Q7Count multiples of 3 or 5Easy

How many numbers from 1 to N are divisible by 3 or by 5?

Example 1
Input: N = 15
Output: 7
Explanation: The numbers are 3, 5, 6, 9, 10, 12 and 15, which is seven in total. 15 is divisible by both, but it is counted once.
Stuck? Open one hint at a time
Hint 1
Start a counter at 0 before the loop.
Hint 2
Inside the loop, add 1 when i % 3 === 0 || i % 5 === 0.
Show all 2 solutions with explanations
Approach 1Loop and count
The idea

Visit every number from 1 to N. Count the ones that pass the test.

JavaScript
function countMultiples(N) {  let count = 0;  for (let i = 1; i <= N; i++) {    if (i % 3 === 0 || i % 5 === 0) count++;  }  return count;}console.log(countMultiples(15)); // 7
How it works

This is a counting accumulator. The || (OR) makes one yes or no decision per number, so a number that is divisible by both is counted once.

Approach 2Count with a formula (inclusion–exclusion)
The idea
  1. Multiples of 3 up to N: Math.floor(N / 3).
  2. Multiples of 5: Math.floor(N / 5).
  3. Numbers divisible by both (multiples of 15) were counted twice, so subtract Math.floor(N / 15) once.
JavaScript
function countMultiples(N) {  return Math.floor(N / 3) + Math.floor(N / 5) - Math.floor(N / 15);}console.log(countMultiples(15));  // 7console.log(countMultiples(100)); // 47
How it works

For N = 15: 5 multiples of 3, plus 3 multiples of 5, minus 1 multiple of 15, gives 7. No loop is needed, so it is instant for any N.

Which approach should you use?

Start with the loop (Approach 1), because it is easy to get right. If the interviewer says N can be a billion, the formula (Approach 2) is the answer they want.

Q8FizzBuzz, 1 to NEasy

For every number from 1 to N, print Fizz (divisible by 3), Buzz (divisible by 5), FizzBuzz (divisible by both) or the number itself. This is LeetCode 412.

Example 1
Input: N = 15
Output:
1
2
Fizz
4
Buzz
…
14
FizzBuzz
Explanation: One line per number. Multiples of 3 become Fizz, multiples of 5 Buzz, multiples of both FizzBuzz.
Stuck? Open one hint at a time
Hint 1
You already solved FizzBuzz for one number in Lesson 3.
Hint 2
Put that code inside a loop from 1 to N.
Show all 2 solutions with explanations
Approach 1else-if chain inside a loop
The idea

The answer from Lesson 3 for a single number, now run for every number from 1 to N.

JavaScript
const N = 15;for (let i = 1; i <= N; i++) {  if (i % 15 === 0) console.log("FizzBuzz");  else if (i % 3 === 0) console.log("Fizz");  else if (i % 5 === 0) console.log("Buzz");  else console.log(i);}/* Output:12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz*/
How it works

Divisible by both 3 and 5 is the same as divisible by 15, so the first check can be shorter.

Approach 2Counters instead of %
The idea

Keep two counters that count up to 3 and 5 and then go back to 0. No division is needed at all.

JavaScript
const N = 6;let three = 0, five = 0;for (let i = 1; i <= N; i++) {  three++;  five++;  let word = "";  if (three === 3) { word += "Fizz"; three = 0; }  if (five === 5) { word += "Buzz"; five = 0; }  console.log(word || i);}/* Output:12Fizz4BuzzFizz*/
How it works

word || i gives the word if it is not empty, and otherwise gives the number. (An empty string is falsy, from Lesson 3.) Interviewers sometimes ask this as a follow-up: “solve it without using %”.

Which approach should you use?

Approach 1 is the standard answer. Approach 2 is useful if the interviewer adds the rule “no % allowed”.

Q9First N Fibonacci numbersMedium

Each Fibonacci number is the sum of the two numbers before it, starting with 0 and 1. Print the first N numbers on one line.

Example 1
Input: N = 7
Output: 0 1 1 2 3 5 8
Explanation: Start with 0 and 1. Each next number is the sum of the two numbers before it: 0+1=1, 1+1=2, 1+2=3, 2+3=5, 3+5=8.
Example 2
Input: N = 1
Output: 0
Explanation: Only the first number.
Stuck? Open one hint at a time
Hint 1
You only need the previous two numbers each time. Keep them in two variables, a and b.
Hint 2
Each step: print a, compute next = a + b, then move both forward: a = b, b = next.
Show all 2 solutions with explanations
Approach 1Two variables that slide forward
The idea

a is the current number and b is the next one. After printing a, move both one place forward.

JavaScript
function fibonacci(N) {  let a = 0, b = 1;  let result = "";  for (let i = 1; i <= N; i++) {    result += a + " ";    const next = a + b;    a = b;    b = next;  }  return result.trim();}console.log(fibonacci(7)); // 0 1 1 2 3 5 8console.log(fibonacci(1)); // 0
How it works
Dry run · fibonacci(7)
iprint anext = a + ba, b after
1011, 1
2121, 2
3132, 3
4253, 5
5385, 8
65138, 13
782113, 21
Approach 2Keep the whole sequence in a list
The idea

Store every number in an array (a list). Build each new number from the last two entries.

JavaScript
function fibonacci(N) {  const fib = [0, 1];  for (let i = 2; i < N; i++) {    fib.push(fib[i - 1] + fib[i - 2]);  }  return fib.slice(0, N).join(" ");}console.log(fibonacci(7)); // 0 1 1 2 3 5 8console.log(fibonacci(1)); // 0
How it works

Arrays are covered in Lesson 8. push adds an item to the end. fib[i - 1] reads the item at a position. slice(0, N) keeps the first N items. join(" ") turns the list into one string with spaces. This uses more memory, but you can look up any earlier number.

Which approach should you use?

Approach 1 uses only two variables, however big N is, so give it in an interview. Approach 2 is useful when you need to look back at earlier values. This is the idea behind dynamic programming (Lesson 54), a method that saves the answers to small problems and reuses them.

Q10Is it prime?Medium

A prime is a whole number greater than 1 that is divisible only by 1 and itself. Return whether n is prime.

Example 1
Input: 7
Output: true
Explanation: 7 is divisible only by 1 and 7.
Example 2
Input: 12
Output: false
Explanation: 12 is divisible by 2, 3, 4 and 6.
Example 3
Input: 1
Output: false
Explanation: By definition a prime must be greater than 1.
Stuck? Open one hint at a time
Hint 1
Try dividing n by every number from 2 to n − 1. If any of them divides n exactly (with remainder 0), n is not prime.
Hint 2
As soon as you find one divisor, you know the answer — stop the loop with break.
Hint 3
You only need to try divisors up to √n (the square root of n). Why? Divisors come in pairs, and one number in each pair is at most √n.
Show all 3 solutions with explanations
Approach 1Try every divisor from 2 to n − 1
The idea

Assume n is prime. Try each divisor. If one divides n exactly, change the answer to “not prime” and stop.

JavaScript
function isPrime(n) {  if (n < 2) return false;  let prime = true;              // a "flag": yes until proven no  for (let d = 2; d < n; d++) {    if (n % d === 0) {      prime = false;      break;                     // one divisor is enough    }  }  return prime;}console.log(isPrime(7));  // trueconsole.log(isPrime(12)); // falseconsole.log(isPrime(1));  // false
How it works

The flag variable prime (a true/false variable that records a yes or no answer) starts as true. It is switched to false as soon as a divisor is found. For a prime like 97, this checks 95 divisors.

Approach 2Stop at the square root
The idea

If n = a × b, one of a and b is at most √n. So if no number up to √n divides n, no larger number can divide it either.

JavaScript
function isPrime(n) {  if (n < 2) return false;  for (let d = 2; d * d <= n; d++) {    if (n % d === 0) return false;  }  return true;}console.log(isPrime(97));  // trueconsole.log(isPrime(91));  // false
How it works

d * d <= n means “d is at most √n”, and it needs no square root calculation. For 97 that is only 8 divisors (2 to 9) instead of 95. For 91, the loop finds 7 (91 = 7 × 13).

Approach 3Square root, odd divisors only
The idea

Handle 2 on its own. After that, no even number can divide an odd n, so try only odd divisors.

JavaScript
function isPrime(n) {  if (n < 2) return false;  if (n % 2 === 0) return n === 2;   // 2 is the only even prime  for (let d = 3; d * d <= n; d += 2) {    if (n % d === 0) return false;  }  return true;}console.log(isPrime(2));   // trueconsole.log(isPrime(97));  // trueconsole.log(isPrime(100)); // false
How it works

This halves the work again. The update d += 2 visits 3, 5, 7, 9, and so on.

Which approach should you use?

Give Approach 2 in an interview. It is short and much faster than Approach 1. Mention Approach 3 as a further improvement. Lesson 13 shows how to find all primes up to N at once with the Sieve of Eratosthenes.

Make it stick

Make it stick — close this page, then
  1. Write the order of execution from memory: start, check, body, update, and so on. Where does the loop end?
  2. Without running it: how many times does for (let i = 3; i <= 9; i++) run? And for (let i = 0; i < 9; i += 3)? (7 and 3.)
  3. Write factorial from scratch and dry-run it for n = 4 in a table.

Revisit schedule: solve two of the questions again tomorrow, again in 3 days, and once more in a week — without looking at the answers. Each time it will feel easier. That is the memory forming.

Next lesson: the while loop, which you use when you do not know in advance how many times to repeat. You will also learn to take a number apart digit by digit.

Practise on LeetCode

Real interview problems that use exactly what this lesson teaches. Solve them in JavaScript after the practice questions above.

Published by Jainik · Last updated · How these lessons are made

Keep learning

Related series