Java Script Hacker Rank 2021 Certification (Chamion League Teams) Solution


Put this in your code as :

const https = require('https');

const fetch = (url) => {

  return new Promise((resolve, reject) => {

    https

      .get(url, (resp) => {

        let data = '';


        // A chunk of data has been recieved.

        resp.on('data', (chunk) => {

          data += chunk;

        });


        // The whole response has been received. Print out the result.

        resp.on('end', () => {

          resolve(JSON.parse(data));

        });

      })

      .on('error', (err) => {

        reject(err.message);

      });

  });

};


const getAPIURL = (year, page) => {

  return `https://jsonmock.hackerrank.com/api/football_matches?competition=UEFA%20Champions%20League&year=${year}&page=${page}`;

};


const getFootballMatches = (year, page) => {

  const url = getAPIURL(year, page);

  return new Promise((resolve, reject) => {

    fetch(url)

      .then((jsonRespone) => resolve(jsonRespone))

      .catch((e) => reject(e.message));

  });

};


async function getTeams(year, k) {

  // write your code here

  // API endpoint template: https://jsonmock.hackerrank.com/api/football_matches?competition=UEFA%20Champions%20League&year=<YEAR>&page=<PAGE_NUMBER>

  const matchesPerTeam = {};

  let initialPage = 1;

  let totalPages = 1;

  while (initialPage <= totalPages) {

    const { total_pages, data: matches } = await getFootballMatches(

      year,

      initialPage,

    );


    matches.forEach(({ team1, team2 }) => {

      matchesPerTeam[team1] = (matchesPerTeam[team1] || 0) + 1;

      matchesPerTeam[team2] = (matchesPerTeam[team2] || 0) + 1;

    });

    totalPages = total_pages;

    initialPage += 1;

  }

  return Object.entries(matchesPerTeam)

    .filter(([, numOfMatches]) => numOfMatches >= k)

    .map(([team]) => team)

    .sort();

}

Comments

  1. Hlw sir please can you give me the javascript weekday coding ?

    ReplyDelete

Post a Comment

Popular posts from this blog

Introduction to Structured Query Language (SQL) Query Solution || Coursera 2022 || Week

HackerRank Day 6: Javascript Dates 10 Days of javascript problem solution.