JavaScript generates simple arithmetic differential series and javascript arithmetic differential Series
The question is very simple. The simplest way is to use the for loop.
let arr = []for (let i = 0; i < b - a + 1; i++) { arr.push(i + a)}return arr
Advanced
When I think about it later, I feel that the previous method is a bit stupid. Then I come up with these methods again.
Array vacancy
Join () and toString () will regard the blank space as undefined (string form ):
// Splice> split> mapArray (B-a + 1 ). join (''). split (''). map (e, I) => a + I) // convert string> split> mapArray (B-a + 1 ). toString (). split (','). map (e, I) => a + I)
Use the Array. from method to implement:
// Convert an empty Array to an Array. from (Array (B-a + 1 )). map (e, I) => a + I) // convert objects similar to arrays. from ({length: B-a + 1 }). map (e, I) => a + I) Array. from ({length: B-a + 1}, (e, I) => a + I)
The extension operators of ES6 can help us complete this task more conveniently.
[... Array (B-a + 1)]. the map (e, I) => a + I) fill (), entries (), and keys () methods do not ignore the empty Array (B-a + 1 ). fill (''). map (e, I) => a + I )[... array (B-a + 1 ). entries ()]. map (e => e [0] + )[... array (B-a + 1 ). keys ()]. map (e => e +)
There are other ways to do this, such as findIndex (), find (),...
Summary
The above section describes how to generate a simple differential series using JavaScript, which is helpful to you. If you have any questions, please leave a message and the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!