Created
September 15, 2020 07:36
-
-
Save phatnguyenuit/46ed74d908b8df6280482b516a1026e8 to your computer and use it in GitHub Desktop.
HackerRank: JavaScript (Basic) on 15th Sep 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
function main(year, k) { | |
getTeams(year, k) | |
.then((result) => console.log(result)) | |
.catch((e) => console.error(e.message)); | |
} | |
main(2015, 6); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const VALID_STATES = ['completed', 'active', 'others']; | |
class NotesStore { | |
//add your code here | |
constructor() { | |
this.notes = []; | |
} | |
addNote(state, name) { | |
if (!name) { | |
throw new Error('Name cannot be empty'); | |
} | |
if (!VALID_STATES.includes(state)) { | |
throw new Error(`Invalid state ${state}`); | |
} | |
this.notes.push({ name, state }); | |
} | |
getNotes(state) { | |
if (!VALID_STATES.includes(state)) { | |
throw new Error(`Invalid state ${state}`); | |
} | |
return this.notes | |
.filter((note) => note.state === state) | |
.map((note) => note.name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment