skip to Main Content

I’m trying to find the sum total of the arrays nested inside of the parent array. and push the sums into their own array. only using the for loop.

I could be way off here, I can’t seem to get past how to find the sum of the second array i know how to find the sum of just one, but when another is thrown into the mix it gets a little dicy and starts flagging errors. any suggestions or thought exercises are more than welcome!

let array = [[1,3,3,5,6],[1,3,6,5,6]];
let sum = 0;
let newArray = []
for(let i = 0; i < array.length; i++){
  sum+=array[i]
  newArray.push(sum)
}
console.log (newArray)
// expected output should be 'newArray =[18,21]'

3

Answers


  1. There are two things incorrect with your code.

    1. The way you are instantiating the array ‘array’ is incorrect. You want an array of arrays. Therefore you must use brackets to indicate the top level array, the same way you used brackets to indicate that the two inner arrays are arrays. let array = [[1,3,3,5,6],[1,3,6,5,6]]; should do the trick.

    2. You have a 2-dimensional array by definitions (an array of arrays), so you can’t have a 1-dimensional for loop. You must iterate over the two dimensions by nesting another for loop within your current one. The outermost for loop will iterate over each array, and the inner loop will iterate over each element at the given array. See: Iterate through 2 dimensional array

    Login or Signup to reply.
  2. You’re looping over the amount of outer arrays, not the inner ones. So sum+= does not calculate the sum.

    USing this:
    How to find the sum of an array of numbers, you can do it like so

    let array = [[1,3,3,5,6],[1,3,6,5,6]];
    let sum = 0;
    let newArray = []
    for(let i = 0; i < array.length; i++){
      sum = array[i].reduce((partialSum, a) => partialSum + a, 0);
      newArray.push(sum)
    }
    console.log (newArray)
    // expected output should be 'newArray =[18,21]'

    But then you could use another map to make it a lot more readable:

    const array = [[1,3,3,5,6],[1,3,6,5,6]];
    const sum = (a) => a.reduce((partialSum, a) => partialSum + a, 0);
    const newArray = array.map(sum);
    
    console.log (newArray)
    Login or Signup to reply.
  3. If you want to use a for-loop, you can do this, but it is not advised.

    Note: This is a very naive; non-idiomatic approach.

    let array = [[1, 3, 3, 5, 6], [1, 3, 6, 5, 6]];
    
    let newArray = new Array(array.length);
    for (let r = 0; r < array.length; r++) {
      let sum = 0;
      for (let c = 0; c < array[r].length; c++) {
        sum += array[r][c];
      }
      newArray[r] = sum;
    }
    
    console.log(newArray); // [18, 21]

    Alternatively, you can initialize the array with zeros. This saves the hassle of setting up an intermediary sum counter.

    let array = [[1, 3, 3, 5, 6], [1, 3, 6, 5, 6]];
    
    let newArray = new Array(array.length).fill(0);
    for (let r = 0; r < array.length; r++) {
      for (let c = 0; c < array[r].length; c++) {
        newArray[r] += array[r][c];
      }
    }
    
    console.log(newArray); // [18, 21]

    Functional approach

    In a purely functional approach to this problem, break it down into smaller pieces.

    1. How do I add two numbers? (simple add reducer function)
    2. How can I add multiple numbers? (reduce the array with the reducer, initialized at 0)
    3. How can I do this for a matrix? (map over the array and call the sum reducer)
    const add = (a, b) => a + b
    const sum = (n) => n.reduce(add, 0)
    
    let data = [[1, 3, 3, 5, 6], [1, 3, 6, 5, 6]]
    let sums = data.map(sum)
    
    console.log(sums) // [18, 21]

    Here it is again, in one-shot:

    let data = [[1, 3, 3, 5, 6], [1, 3, 6, 5, 6]]
    let sums = data.map((n) => n.reduce((a, b) => a + b, 0))
    
    console.log(sums) // [18, 21]
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search