-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutilities-array.js
27 lines (26 loc) · 977 Bytes
/
utilities-array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// --------------------------------------------------------------------------
// -- utilities-array.js
// -- initial author: Renick Bell ([email protected])
// -- initial creation date: Fri Jul 7 10:57 AM CST 2023
// -- contributors: Yiler Huang ([email protected]); Steve Wang ([email protected])
// -- license: GPL 3.0
// --------------------------------------------------------------------------
function buildArray(n, fillFunction) {
let outputArray = [];
for (let i = 0; i < n; i++) {
outputArray.push(fillFunction(i))
}
return outputArray
}
//Generated with Chatgpt:
function adjustArrayLength(number, array) {
const arrayLength = array.length;
if (arrayLength === number) {
return array;
} else if (arrayLength < number) {
const numCopies = Math.ceil(number / arrayLength);
return array.concat(...Array(numCopies).fill(array)).slice(0, number);
} else {
return array.slice(0, number);
}
}