Arithmetic Progression in JavaScript

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") subgraph Lab Skills javascript/variables -.-> lab-28138{{"`Arithmetic Progression in JavaScript`"}} javascript/data_types -.-> lab-28138{{"`Arithmetic Progression in JavaScript`"}} javascript/arith_ops -.-> lab-28138{{"`Arithmetic Progression in JavaScript`"}} javascript/comp_ops -.-> lab-28138{{"`Arithmetic Progression in JavaScript`"}} end

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.

Other JavaScript Tutorials you may like