-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
28 lines (24 loc) · 1 KB
/
script.js
File metadata and controls
28 lines (24 loc) · 1 KB
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
28
/**
* Return every second element from `arr`.
* @param {Array} arr - input array (commonly 100 integers)
* @param {number} [start=1] - parity to start from: 0 => indices 0,2,4...; 1 => indices 1,3,5...
* @returns {Array} selected elements
*/
function getEverySecond(arr, start = 1) {
if (!Array.isArray(arr)) throw new TypeError("arr must be an array");
start = Number(start);
if (!Number.isInteger(start) || (start !== 0 && start !== 1)) {
throw new TypeError("start must be 0 or 1");
}
return arr.filter((_, i) => (i % 2) === start);
}
// Example: array of integers from 1 to 100
const nums = Array.from({ length: 100 }, (_, i) => i + 1);
// Get every second number (starting from the 2nd element, i.e. indices 1,3,5,...)
const result = getEverySecond(nums);
console.log("Every second number from 1..100 (starting at index 1):");
console.log(result);
// export for Node.js usage / tests
if (typeof module !== "undefined" && module.exports) {
module.exports = { getEverySecond };
}