# You can simplify this code?

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;
}``````
April 19th 20 at 12:47
April 19th 20 at 12:49
Solution
``````sumsInArray function(arr) {
return arr.map(innerArr => innerArr.reduce((a, b) => (a + b), 0));
}``````
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.
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;
}``````
Then it is better so:

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

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

Find more questions by tags JavaScript