{"size": 1644, "ext": "js", "max_stars_repo_path": "7-kyu/Average Array/index.js", "max_stars_repo_name": "ayessetova/codeW", "max_stars_repo_stars_event_min_datetime": "2017-12-08T16:51:31.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-30T01:59:49.000Z", "max_issues_repo_path": "7-kyu/Average Array/index.js", "max_issues_repo_name": "ayessetova/codeW", "max_issues_repo_issues_event_min_datetime": "2018-04-27T03:54:20.000Z", "max_issues_repo_issues_event_max_datetime": "2020-12-06T20:13:40.000Z", "max_forks_repo_path": "7-kyu/Average Array/index.js", "max_forks_repo_name": "ayessetova/codeW", "max_forks_repo_forks_event_min_datetime": "2017-12-18T18:19:13.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-29T12:16:38.000Z"}
{"max_stars_count": 314, "max_issues_count": 4, "max_forks_count": 141, "avg_line_length": 28.8421052632, "max_line_length": 182, "alphanum_fraction": 0.551703163}
/* eslint no-return-assign: 0 */
/*
  Title:
    Average Array

  Description:
    Create a function that takes a 2D array as an input, and outputs another array that contains the average values for the numbers in the nested arrays at the corresponding indexes.

  Examples:
    [ [1, 2, 3, 4], [5, 6, 7, 8] ]  ==>  [3, 4, 5, 6]

    1st array: [1, 2, 3, 4]
    2nd array: [5, 6, 7, 8]
                |  |  |  |
                v  v  v  v
    average:   [3, 4, 5, 6]

    And another one:
    [ [2, 3, 9, 10, 7], [12, 6, 89, 45, 3], [9, 12, 56, 10, 34], [67, 23, 1, 88, 34] ]
    average: ==>  [22.5, 11, 38.75, 38.25, 19.5]

    1st array: [  2,   3,    9,   10,    7]
    2nd array: [ 12,   6,   89,   45,    3]
    3rd array: [  9,  12,   56,   10,   34]
    4th array: [ 67,  23,    1,   88,   34]
                  |    |     |     |     |
                  v    v     v     v     v
    average:   [22.5, 11, 38.75, 38.25, 19.5]

  Notes:
    The function should also work with negative numbers and floats.

  Kata Link:
    https://www.codewars.com/kata/average-array

  Discuss Link:
    https://www.codewars.com/kata/average-array/discuss

  Solutions Link:
    https://www.codewars.com/kata/average-array/solutions
*/

// Long Solution
const avgArray = array => {
  const initialArray = Array.from({ length: array[0].length }, () => 0)
  const totalValues = array.reduce((result, array) => {
    array.forEach((value, index) => (result[index] += value))
    return result
  }, initialArray)

  const totalAverages = totalValues.map(value => value / array.length)
  return totalAverages
}

// Function Export
module.exports = avgArray
