Thrice sum of elements of array - JavaScript

We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of three consecutive elements from the original array.

For example, if the input array is −

const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Then the output should be −

const output = [3, 12, 21, 9];

Example

Following is the code −

const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const thriceSum = arr => {
   if(!arr.length){
      return [];
   }
   const res = [];
   for(let i = 0; i 

Output

This will produce the following output in console −

[ 3, 12, 21, 9 ]
Updated on: 2020-09-30T13:53:52+05:30

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements