├── javascript-basic-certificate.PNG ├── weekdayText.js ├── README.md └── footballMatches.js /javascript-basic-certificate.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MJavedAli/hackerrank-javascript-basic-certification/HEAD/javascript-basic-certificate.PNG -------------------------------------------------------------------------------- /weekdayText.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | const https = require('https'); 5 | 6 | process.stdin.resume(); 7 | process.stdin.setEncoding('utf-8'); 8 | 9 | let inputString = ''; 10 | let currentLine = 0; 11 | 12 | process.stdin.on('data', function (inputStdin) { 13 | inputString += inputStdin; 14 | }); 15 | 16 | process.stdin.on('end', function () { 17 | inputString = inputString.split('\n'); 18 | main(); 19 | }); 20 | 21 | function readLine() { 22 | return inputString[currentLine++]; 23 | } 24 | 25 | function weekdayText(weekdays) { 26 | return function getText(target) { 27 | return weekdays[target] || (function () { 28 | throw new Error(`Invalid weekday number`) 29 | }()); 30 | } 31 | } 32 | function main() { 33 | const ws = fs.createWriteStream(process.env.OUTPUT_PATH); 34 | const weekdays = readLine().trim().split(' '); 35 | const getText = weekdayText(weekdays); 36 | try { 37 | const value = getText(parseInt(readLine().trim())); 38 | ws.write(value); 39 | } catch (e) { 40 | ws.write(`${e}`); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hackerrank-javascript-basic-certification 2 | 3 | ![image](https://user-images.githubusercontent.com/30997178/158071495-6929e045-2e4f-4219-949d-c34bdce92092.png) 4 | 5 | ## 1. JavaScript: Champions League Teams 6 | 7 | In this challenge, the given REST API cDntains information about football matcnes played in the 8 | years 2011-2015. 9 | 10 | Given a year and integer k, denoting the minimum number of matchE's we are interested in, your task 11 | is to use the API to get the names of teams that played at least k matches in the given year in the 12 | competition named UEFA Champions League. The names must be returned as an array and Drdered in 13 | ascending order. 14 | 15 | The given API uses pagination to return tne data divided into pages. Fetching the whole data 16 | available on the API requires multiple requests. 17 | 18 | To get a single page of matches played in UEFA Champions League in the given year, perform HTTP GET 19 | request to: https://jsonmock.hackerrank.com/api/footballs matches? 20 | competition=UEFA9ti20Champions9620League&y ear=«year»&page=«pageNumber» 21 | where denotes the year of the match and 22 | is an integer denoting the page of the results we are requesting. 23 | 24 | ## 2. JavaScript: Weekday Text 25 | 26 | Implement the function weekdayText such that: 27 | 28 | • It takes a singlé argument. weekdays,.which is an array of string 29 | • It returns'a new function that called getText that takes a single integer argument. number, and 30 | does the following. 31 | a. It returns the value from theweekdays array at 32 | that 0-based index number. 33 | 34 | b. If number is out of range, the function throws an Error object 35 | with the message of 'Invalid weekday number'. 36 | -------------------------------------------------------------------------------- /footballMatches.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const fs = require('fs'); 3 | const https = require('https'); 4 | 5 | process.stdin.resume(); 6 | process.stdin.setEncoding('utf-8'); 7 | 8 | let inputString = ''; 9 | let currentLine = 0; 10 | 11 | process.stdin.on('data', function (inputStdin) { 12 | inputString += inputStdin; 13 | }); 14 | 15 | process.stdin.on('end', function () { 16 | inputString = inputString.split('\n'); 17 | 18 | main(); 19 | }); 20 | 21 | function readLine() { 22 | return inputString[currentLine++]; 23 | } 24 | 25 | const fs = require('fs'); 26 | const https = require('https'); 27 | 28 | process.stdin.resume(); 29 | process.stdin.setEncoding('utf-8'); 30 | 31 | let inputString = ''; 32 | let currentLine = 0; 33 | 34 | process.stdin.on('data', function (inputStdin) { 35 | inputString += inputStdin; 36 | }); 37 | 38 | process.stdin.on('end', function () { 39 | inputString = inputString.split('\n'); 40 | 41 | main(); 42 | }); 43 | 44 | function readLine() { 45 | return inputString[currentLine++]; 46 | } 47 | 48 | 49 | const fetch = (url) => { 50 | return new Promise((resolve, reject) => { 51 | https 52 | .get(url, (resp) => { 53 | let data = ''; 54 | 55 | resp.on('data', (chunk) => { 56 | data += chunk; 57 | }); 58 | 59 | resp.on('end', () => { 60 | resolve(JSON.parse(data)); 61 | }); 62 | }) 63 | .on('error', (err) => { 64 | reject(err.message); 65 | }); 66 | }); 67 | }; 68 | 69 | const getAPIURL = (year, page) => { 70 | return `https://jsonmock.hackerrank.com/api/football_matches?competition=UEFA%20Champions%20League&year=${year}&page=${page}`; 71 | }; 72 | 73 | const getFootballMatches = (year, page) => { 74 | const url = getAPIURL(year, page); 75 | return new Promise((resolve, reject) => { 76 | fetch(url) 77 | .then((jsonRespone) => resolve(jsonRespone)) 78 | .catch((e) => reject(e.message)); 79 | }); 80 | }; 81 | async function getTeams(year, k) { 82 | // write your code here 83 | // API endpoint template: https://jsonmock.hackerrank.com/api/football_matches?competition=UEFA%20Champions%20League&year=&page= 84 | 85 | const matchesPerTeam = {}; 86 | let initialPage = 1; 87 | let totalPages = 1; 88 | while (initialPage <= totalPages) { 89 | const { total_pages, data: matches } = await getFootballMatches( 90 | year, 91 | initialPage, 92 | ); 93 | 94 | matches.forEach(({ team1, team2 }) => { 95 | matchesPerTeam[team1] = (matchesPerTeam[team1] || 0) + 1; 96 | matchesPerTeam[team2] = (matchesPerTeam[team2] || 0) + 1; 97 | }); 98 | totalPages = total_pages; 99 | initialPage += 1; 100 | } 101 | return Object.entries(matchesPerTeam) 102 | .filter(([, numOfMatches]) => numOfMatches >= k) 103 | .map(([team]) => team) 104 | .sort(); 105 | } 106 | async function main() { 107 | const ws = fs.createWriteStream(process.env.OUTPUT_PATH); 108 | 109 | const year = parseInt(readLine().trim()); 110 | const k = parseInt(readLine().trim()); 111 | 112 | const teams = await getTeams(year, k); 113 | 114 | for (const team of teams) { 115 | ws.write(`${team}\n`); 116 | } 117 | } 118 | --------------------------------------------------------------------------------