如何以前一個值之和的步長回傳一個陣列?
我有一個輸入數字 1000 我想得到 [1000, 2000, 3000 ... 10000]
我試圖:
const range = (i: number, o: number) => {
const arr = [0, 1, 2, 3, 4].reduce(
(accumulator: any, currentValue: any, index: any, array: any) => {
console.log((array[index] = accumulator currentValue));
return 1;
},
10000
);
};
range(1000, 10000);
uj5u.com熱心網友回復:
使用Array.from并提供第一個引數作為具有length等于步長的屬性的物件,第二個引數作為簡單地乘以索引和步長的映射函式:
const range = (start,end) =>
Array.from( {length: end/start} ,(_,i) => (i 1) * start )
console.log(range(1000,10000))
uj5u.com熱心網友回復:
嘗試這個:
function range(start, end) {
var arr = []; // Start with an empty array
/* Start a counter at the start value, and increment it
* by the start value while it is less than or equal to
* the end value. Push this value into arr each time
*/
for (let i = start; i <= end; i = start) {
arr.push(i);
}
return arr; // Return the array
}
uj5u.com熱心網友回復:
使用簡單回圈并檢查是否number * index <= max在向新陣列添加值時:
const range = (number, max) => {
let index = 1;
let newArray = [];
do {
newArray.push(number * index);
index ;
} while (number * index <= max);
return newArray;
};
console.log(range(1000, 10000));
console.log(range(10000, 10000));
uj5u.com熱心網友回復:
const range = (start, end) => {
let acc = [];
for (let i = start; i <= end; i = i start) {
acc = [...acc, i];
}
return acc;
}
如果不是,請詳細說明您的示例以清楚說明...step of the sum of the previous value.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/370405.html
標籤:javascript
上一篇:如何檢查用戶是否存在于貓鼬中?
