B
B
BB 82018-03-21 21:24:58
JavaScript
BB 8, 2018-03-21 21:24:58

Is it possible to display the sum of the elements of two arrays in one action?

function sumTwoArrays(arr1, arr2) {
  let sum1 = arr1.reduce((sum, cur) => sum + cur);
  let sum2 = arr2.reduce((sum, cur) => sum + cur);
  return sum1 + sum2;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RidgeA, 2018-03-21
@fruity4pie

it is possible, if there are 2 elements in the sum of two arrays, then the action will be one. This is if we talk exclusively about mathematical operations.
If you want to write all this in one line, you can do concat and only then reduce

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question