船员公众号
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
997 B

2 months ago
// @ts-nocheck
/**
*
* @param start
* @param end
* @param step 1
* @param fromRight false
* @returns
*/
export function range(start: number, end: number, step: number = 1, fromRight: boolean = false): number[] {
let index = -1;
// 计算范围的长度
let length = Math.max(Math.ceil((end - start) / (step || 1)), 0);
// 创建一个长度为 length 的数组
const result = new Array(length);
// 使用循环生成数字范围数组
while (length--) {
// 根据 fromRight 参数决定从左侧还是右侧开始填充数组
result[fromRight ? length : ++index] = start;
start += step;
}
return result;
}
// 示例
// console.log(range(0, 5)); // 输出: [0, 1, 2, 3, 4]
// console.log(range(1, 10, 2, true)); // 输出: [9, 7, 5, 3, 1]
// console.log(range(5, 0, -1)); // 输出: [5, 4, 3, 2, 1]