You can simplify this code to solve this problem?

**Here is the code:**

```
sumsInArray function(arr){
const newArr = [];
for (let i = 0; i < arr.length; i++){
let sum = 0;
if (arr[i].length === 0) newArr.push(sum);
for (let j = 0; j < arr[i].length; j++){
sum += arr[i][j];
if (j === arr[i].length - 1) {
newArr.push(sum);
sum = 0;
}
}
}
return newArr;
}
```

asked April 19th 20 at 12:47

3 answers

answered on

Solution

```
sumsInArray function(arr) {
return arr.map(innerArr => innerArr.reduce((a, b) => (a + b), 0));
}
```

answered on April 19th 20 at 12:51

Can be reduced, it is possible to win back the drop of the time. But to simplify unlikely. In the plan, it was clear that the man who badly fumbles in JS)

See here. And then in the left sidebar, there are many interesting things that can cut the recording.

See here. And then in the left sidebar, there are many interesting things that can cut the recording.

answered on April 19th 20 at 12:53

Here he has simplified to two lines who cares) One more nuance and less two lines **if**)

```
sumsInArray unction(arr){
const arrNew = [];
let sum;
for (let i = 0; i < arr.length; i++) {
sum = 0;
for (let j = 0; j < arr[i].length; j++) {
sum = sum + arr[i][j];
}
arrNew.push(sum);
}
return arrNew;
}
```

Find more questions by tags JavaScript

Well, a word of advice: chase not necessary for a small number of rows, and the optimality of the algorithm. - Kaleb31 commented on April 19th 20 at 12:56