Course: JavaScript

Progress (0%)

Exercise: Sequence In Range

Exercise 11 Average

Prerequisites for the exercise

  1. JavaScript Numbers — Basics


Create a function printSequence() to print a sequence, given three parameters defining it.


Sequences of integers are really an exciting topic in mathematics. And in this regard, arithmetic sequences are a particularly popular candidate to consider.

An arithmetic sequence starts off at an integer ::a::, known as the initial term, and proceeds with an increment ::d:: (sometimes also referred to as the common difference between subsequent terms).

For instance, the sequence of positive integers starting at ::0:: is shown below:

::0, 1, 2, 3, 4, ...::

This is an arithmetic sequence with ::a = 1:: and ::d = 1::.

Similarly, we can have a sequence of the multiples of ::3:: beginning at ::12::, as follows:

::12, 15, 18, 21, 24, ...::

Here ::a = 12:: and ::d = 3::.

The common difference can also be negative. For example, consider the sequence below:

::10, 9, 8, 7, ...::

Here ::a = 10:: and ::d = -1::.

Notice how all the sequences presented thus far are infinite. We can, however, reduce them all to a finite subsequence by using a range.

Given a sequence with initial term ::a:: and common difference ::d::, we want to consider the finite subsequence in the range ::[a, b):: when ::b \gt a:: or in the range ::(b, a]:: when ::a \lt b::.

For example, when ::a = 0::, ::d = 1::, and ::b = 5::, here's what we get:

::0, 1, 2, 3, 4::

::5:: is not part of the sequence because the range is ::[0, 5)::, that is, ::0 \le x \lt 5::

Similarly, when ::a = 5::, ::d = -1::, and ::b = 0::, we get the following:

::5, 4, 3, 2, 1::

See how the sequence is decreasing since ::d = -1::. The range is ::(0, 5]:: and likewise ::0:: is not part of the sequence.

In this exercise, you have to create a function printSequence() that takes in three parameters defining ::a::, ::b:: and ::d::, and then prints the entire subsequence of integers on the document.

The signature of the printSequence() function must be as follows:

printSequence(a, b, d)

Notice that after a comes b, not d; d comes the last.

The sequence must be output on the document just as shown above, i.e. starting at ::a::, followed by all the individual elements separated by a comma (,).

For example, if a = 1, b = 5, and d = 1, here's what the program should output:

1, 2, 3, 4

Keep in mind that if the given parameters are such that there is no existing sequence for them, then printSequence() should output the text 'Nothing to print'. There are four such cases to deal with.

For the purpose of this exercise, the printSequence() function should be called once, as soon as the page loads, with the parameters a, b and d obtained via browser prompt inputs.

For simplicity, you should assume that a, b and d are all integer values.

If you assume that they can be anything, such as floats, or non-numbers, then you'll have to implement more checks in the code.

In the end, your program should work as follows:

Live Example

View Solution

New file

Inside the directory you created for this course on JavaScript, create a new folder called Exercise-11-Sequence-In-Range and put the .html solution files for this exercise within it.


Let's start by enumerating the four cases in which there is no sequence to consider and when we must print the message 'Nothing to print'.

Here are these four cases:

  • When d is 0, the sequence isn't progressing; thus, we have our first case.
  • When a is equal to b, there is nothing to consider in the sequence. Thus, we have our second case.
  • When a is less than b, the sequence should be increasing; thus d must be positive. If this is not the case (that is, if d is not positive), we have our third case.
  • When a is greater than b, the sequence should be decreasing; thus d must be negative. If this is not the case (that is, if d is not negative), we have our fourth case.

First, let's implement these four cases using an if conditional and a couple of basic logical operators:

function printSequence(a, b, d) {
   if (d === 0 || a === b || (b < a && d > 0) || (a < b && d < 0)) {
      document.write('Nothing to print');

So far, so good.

Beyond the if conditional, we know that the given parameters a, b and d are fine, and that we must proceed with printing the underlying sequence.

Doing so is very straightforward: just use a for loop, beginning at a, ending at b (exclusive), and incrementing by d (when d is negative, an increment would automatically become a decrement).

This is accomplished below:

function printSequence(a, b, d) {
   if (d === 0 || a === b || (b < a && d > 0) || (a < b && d < 0)) {
      document.write('Nothing to print');
   for (var x = a; Math.sign(d) * (x - b) < 0; x += d) {
      var sep = ', ';
      if (x === a) {
         sep = '';
      document.write(sep + x);

A crucial point worth noting here is the condition of the loop: Math.sign(d) * (x - b) < 0. What is this and why do we use it instead of something like a < b?

Well, sometimes, depending on the values of a, b, and d, the given sequence would be increasing or decreasing. In the case of increasing, we need to use the condition x < b. However, in the latter case, i.e. when the sequence is decreasing, we need to use the condition x > b.

This difference in the operators used in each case means that we can't use either of these expressions, x < b or x > b, without some condition to determine which specific expression to use.

To solve this problem, we can take a number of different approaches. But perhaps the simplest would be to use the sign of d and a slight rearrangement of the inequalities shown above to cater to both the given cases.

  • When d is positive, we need to use x < b which can be rearranged as x - b < 0.
  • When d is negative, we need to use x > b which can be rearranged as -1 * (x - b) < 0.

This means that we can simply multiply the sign of d with (x - b) in order to get the correct expression to compare with < 0. And that's exactly what we do above, employing the Math.sign() method.

Another important thing to note in the code above is how we implement the ', ' separator between individual elements. On all but the first iteration, the separator sep is prepended to x.

This completes the function.

Let's now write the main program, asking the user to input the three parameters and then printing the sequence that corresponds to them.

Fortunately, it's very simple:

function printSequence(a, b, d) { /* ... */ }

var a = Number(prompt('a:'));
var b = Number(prompt('b:'));
var d = Number(prompt('d:'));
printSequence(a, b, d);

Here's how the program works:

Live Example

Perfect! Just as we wanted.

And this completes this exercise.

"I created Codeguage to save you from falling into the same learning conundrums that I fell into."

— Bilal Adnan, Founder of Codeguage