Register to get access to free programming courses with interactive exercises

Conditions inside loops JavaScript fundamentals

Both the loop body and function body are places where statements are executed. So we can use everything we already learned inside it, such as conditionals.

Imagine a function that counts the number of times a letter appears in a sentence. Here's how it might work:

countChars('Fear cuts deeper than swords.', 'e'); // 4
// If there are no matches, the result is 0
countChars('Sansa', 'y'); // 0

Before examining its contents, answer these questions:

  • Is this an aggregate operation?
  • What will be the test condition for whether or not the character occurs?
  const countChars = (str, char) => {
  let i = 0;
  let count = 0;
  while (i < str.length) {
    if (str[i] === char) {
      // Count only relevant characters
      count = count + 1;
    }
    // Counter increases no matter what happens
    i = i + 1;
  }

  return count;
};

This is an aggregate task. Although it doesn't count every character, you still have to parse through each character to calculate the sum.

A key feature of this loop is the condition inside its body. The variable count only increases when the current character matches the expected one.

Apart from that, it is a generic aggregate function that returns the number of required characters.

Returning from loops

Dealing with loops usually comes down to two cases:

  1. Aggregation. Accumulation of data during iterations and handling it after the loop. String reversal is just one of these cases
  2. Executing a loop until you get the required result and breaking from it. For example, if the job of the loop is to find prime numbers. Remember that a prime number is a number that can only be divided without a remainder by itself and by one

Consider a simple algorithm to check prime numbers. We will divide a given number x by all numbers between 2 and x - 1 and check the remainder. If we don't find a divisor that divides the number x without a remainder in this range, then we are looking at a prime number.

If you think about it, it's actually enough to check numbers not up to x - 1, but up to half the given number. For example, 11 is not divisible by 2, 3, 4, or 5. But it's also guaranteed that it can't be divided by numbers greater than its half. So, you can do a little optimization and check division only up to x / 2.

const isPrime = (number) => {
  if (number < 2) {
    return false;
  }

  let divider = 2;

  while (divider <= number / 2) {
    if (number % divider === 0) {
      return false;
    }

    divider += 1;
  }

  return true;
}

isPrime(1); // false
isPrime(2); // true
isPrime(3); // true
isPrime(4); // false

The algorithm is built like so: if during the successive division by numbers up to x / 2, there is at least one result without a remainder, then the given argument is not a prime number, and therefore further computations are pointless. At this point, it returns false.

And only if the entire loop is completed can we say that the number is prime since no number by which it can be divided without a remainder can be found.


Recommended materials

  1. List of prime numbers

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY