用 JavaScript 構建一個數組,其中含有指定數字的前 n 倍數


問題

我們需要編寫一個 Javascript 函式,其中有 2 個數字,比如 m 和 n。

我們的函式應當構建並返回一個數組,其中包含 m 的前 n 個自然倍數。

示例

以下為程式碼 −

 動態演示

const m = 6;
const n = 14;
const firstNMultiple = (m = 1, n = 1) => {
   const res = [];
   for(let i = 1; i <= n; i++){
      const multiple = m * i;
      res.push(multiple);
   };
   return res;
};
console.log(firstNMultiple(m, n));

輸出

[ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84 ]

更新於: 2021 年 4 月 20 日

334 次瀏覽

啟動你的 職業生涯

完成課程並獲得認證

開始
廣告