|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
910 bytes
|
| Line | |
|---|
| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.getEveryNth = getEveryNth;
|
|---|
| 7 | /**
|
|---|
| 8 | * Given an array and a number N, return a new array which contains every nTh
|
|---|
| 9 | * element of the input array. For n below 1, an empty array is returned.
|
|---|
| 10 | * For n equal to 1, the input array is returned as is.
|
|---|
| 11 | * For n greater than the length of the array, an array containing the first element
|
|---|
| 12 | * and every nTh element after that (if any) is returned.
|
|---|
| 13 | *
|
|---|
| 14 | * @param array An input array.
|
|---|
| 15 | * @param n A number specifying which elements to take.
|
|---|
| 16 | * @returns The result array of the same type as the input array.
|
|---|
| 17 | */
|
|---|
| 18 | function getEveryNth(array, n) {
|
|---|
| 19 | if (n < 1) {
|
|---|
| 20 | return [];
|
|---|
| 21 | }
|
|---|
| 22 | if (n === 1) {
|
|---|
| 23 | return array;
|
|---|
| 24 | }
|
|---|
| 25 | var result = [];
|
|---|
| 26 | for (var i = 0; i < array.length; i += n) {
|
|---|
| 27 | var item = array[i];
|
|---|
| 28 | if (item !== undefined) {
|
|---|
| 29 | result.push(item);
|
|---|
| 30 | }
|
|---|
| 31 | }
|
|---|
| 32 | return result;
|
|---|
| 33 | } |
|---|
Note:
See
TracBrowser
for help on using the repository browser.