
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding sum of every nth element of array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns the cumulative sum of every number present at the index that is a multiple of n from the array.
Example
The code for this will be −
const arr = [5, 3, 5, 6, 12, 5, 65, 3, 2]; const num = 3; const nthSum = (arr, num) => { let sum = 0; for(let i = 0; i < arr.length; i++){ if(i % num !== 0){ continue; }; sum += arr[i]; }; return sum; }; console.log(nthSum(arr, num));
Output
The output in the console −
76
- Related Articles
- Reduce an array to the sum of every nth element - JavaScript
- Finding the nth power of array element present at nth index using JavaScript
- JavaScript: take every nth Element of Array and display a fixed number of values?
- How to remove every Nth element from an array JavaScript?
- Finding nth element of an increasing sequence using JavaScript
- Finding nth element of the Padovan sequence using JavaScript
- Finding the nth element of the lucas number sequence in JavaScript
- Finding sum of alternative elements of the array in JavaScript
- Finding sum of a range in an array JavaScript
- Finding the majority element of an array JavaScript
- Finding the sum of unique array values - JavaScript
- Finding missing element in an array of numbers in JavaScript
- Finding desired sum of elements in an array in JavaScript
- Python – Extract Kth element of every Nth tuple in List
- Finding the sum of all numbers in the nth row of an increasing triangle using JavaScript

Advertisements