Introduction
In this lab, we will explore the concept of arithmetic progression in JavaScript. We will learn how to create an array of numbers in the arithmetic progression, starting with a given positive integer and up to a specified limit. By the end of this lab, you will have a solid understanding of how to use the Array.from() and map() methods to create arrays with desired values in the given range.
Arithmetic Progression Code Example
To practice coding, open the Terminal/SSH and type node.
Here is an example code that creates an array of numbers in arithmetic progression. The array starts with a given positive integer and goes up to a specified limit:
const arithmeticProgression = (n, lim) =>
Array.from({ length: Math.ceil(lim / n) }, (_, i) => (i + 1) * n);
To use this code, simply call the function arithmeticProgression with two arguments: the starting positive integer and the limit. For example:
arithmeticProgression(5, 25); // [5, 10, 15, 20, 25]
Summary
Congratulations! You have completed the Arithmetic Progression lab. You can practice more labs in LabEx to improve your skills.