
- 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
Even index sum in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of integers. Our function should return the sum of all the integers that have an even index, multiplied by the integer at the last index.
const arr = [4, 1, 6, 8, 3, 9];
Expected output −
const output = 117;
Example
Following is the code −
const arr = [4, 1, 6, 8, 3, 9]; const evenLast = (arr = []) => { if (arr.length === 0) { return 0 } else { const sub = arr.filter((_, index) => index%2===0) const sum = sub.reduce((a,b) => a+b) const posEl = arr[arr.length -1] const res = sum*posEl return res } } console.log(evenLast(arr));
Output
Following is the console output −
117
- Related Articles
- Odd even index difference - JavaScript
- Find sum of even index binomial coefficients in C++
- Find even odd index digit difference - JavaScript
- Swapping even and odd index pairs internally in JavaScript
- Sum of even Fibonacci terms in JavaScript
- Cumulative sum at each index in JavaScript
- Even numbers at even index and odd numbers at odd index in C++
- Sum of Array Divisible by Size with Even and Odd Numbers at Odd and Even Index in Java
- Reverse index value sum of array in JavaScript
- Greatest sum and smallest index difference in JavaScript
- Common element with least index sum in JavaScript
- Determining sum of array as even or odd in JavaScript
- Swap Even Index Elements And Odd Index Elements in Python
- Sum of even numbers up to using recursive function in JavaScript
- Sum of individual even and odd digits in a string number using JavaScript

Advertisements