30秒学会 JavaScript 片段 · 2022年11月11日

30秒学会 JavaScript 片段 – Arithmetic progression

Creates an array of numbers in the arithmetic progression, starting with the given positive integer and up to the specified limit.

  • Use Array.from() to create an array of the desired length, lim / n. Use a map function to fill it with the desired values in the given range.

代码实现

const arithmeticProgression  = (n, lim) =>
  Array.from({ length: Math.ceil(lim / n) }, (_, i) => (i + 1) * n );

arithmeticProgression(5, 25); // [5, 10, 15, 20, 25]

翻译自:https://www.30secondsofcode.org/js/s/arithmetic-progression