- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to multiply odd index values JavaScript
We are required to write a function, that takes in an array of Number literals as one and the only argument. The numbers that are situated at even index should be returned as it is. But the numbers situated at the odd index should be returned multiplied by their corresponding indices.
For example −
If the input is: [5, 10, 15, 20, 25, 30, 50, 100] Then the function should return: [5, 10, 15, 60, 25, 150, 50, 700]
We will use the Array.prototype.reduce() method to construct the required array and the code for the function will be −
Example
const arr = [5, 10, 15, 20, 25, 30, 50, 100]; const multiplyOdd = (arr) => { return arr.reduce((acc, val, ind) => { if(ind % 2 === 1){ val *= ind; }; return acc.concat(val); }, []); }; console.log(multiplyOdd(arr));
Output
The output in the console will be −
[ 5, 10, 15, 60, 25, 150, 50, 700 ]
- Related Articles
- Odd even index difference - JavaScript
- Find even odd index digit difference - JavaScript
- Python Program to Remove the Characters of Odd Index Values in a String
- Swapping even and odd index pairs internally in JavaScript
- Matching odd even indices with values in JavaScript
- Even numbers at even index and odd numbers at odd index in C++
- How to multiply two Arrays in JavaScript?
- Returning array values that are not odd in JavaScript
- How to multiply corresponding values from two matrices in R?
- Swap Even Index Elements And Odd Index Elements in Python
- How to multiply corresponding values from two data.table objects in R?
- How to multiply corresponding values from two data frames in R?
- How to multiply two matrices in R if they contain missing values?
- How to multiply vector values in sequence with matrix columns in R?
- How to multiply all values in a list by a number in R?

Advertisements