Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Every Nth Element

Numbers, Dates & Timersmedium

Course · Section 12: Numbers, Dates, Intl and Timers · Lecture 183: The Remainder Operator

Implement `everyNth(arr, n)` returning the elements at indices `0, n, 2n, ...` using the remainder operator.

Sample tests

Input: everyNth([10,20,30,40,50,60], 2)
Output: [10,30,50]
Input: everyNth([1,2,3], 1)
Output: [1,2,3]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • An n of 1 keeps every element, which is correct.
Approach & explanation (try first)

Filtering on the index modulo n selects every nth element.

Loading...
⌘/Ctrl + Enter

Run your code to see results.