text: const { NotImplementedError } = require('../extensions/index.js');
  
  /**
   * Given an array of domains, return the object with the appearances of the DNS.
   *
   * @param {Array} domains
   * @return {Object}
   *
   * @example
   * domains = [
   *  'code.yandex.ru',
   *  'music.yandex.ru',
   *  'yandex.ru'
   * ]
   *
   * The result should be the following:
   * {
   *   '.ru': 3,
   *   '.ru.yandex': 3,
   *   '.ru.yandex.code': 1,
   *   '.ru.yandex.music': 1,
   * }
   *
   */
  function getDNSStats( domains ) {
    let rez;
    rez = domains.map((el) => {
      return el.split('.').reverse();
    })
    rez = rez.map((el) => {
      let dns = [];
      for (let index = 0; index < el.length; index++) {
        dns = [...dns, index == 0 ? '.' + el[0] : dns[index - 1] + '.' + el[index]]
      }
      return dns;
    })
  
    let obj = {};
    rez = rez.reduce((wholeArr, cur) => {
      return wholeArr = [...wholeArr, ...cur]
  
    }, []);
  
    rez.forEach(el => {
      if (obj.hasOwnProperty(el)) {
        obj[el] += 1
      } else obj[el] = 1
    });
  
    return obj;
  }
  
  module.exports = {
    getDNSStats
  };
  