Skip to content

Instantly share code, notes, and snippets.

@akshaymittal143
Created October 6, 2018 19:41
Show Gist options
  • Save akshaymittal143/88b803b2010e252f7ea2b22d26f93f67 to your computer and use it in GitHub Desktop.
Save akshaymittal143/88b803b2010e252f7ea2b22d26f93f67 to your computer and use it in GitHub Desktop.
getMovies title
const https = require('https');
/*
* Complete the function below.
* Use console.log to print the result, you should not return from the function.
*/
function getMovieTitles(substr) {
pageNum = 1;
let url = 'https://jsonmock.hackerrank.com/api/movies/search/?Title=' + substr + "&page=" + pageNum;
https.get(url, (res) => {
res.setEncoding('utf8');
res.on('data', function(body) {
let dataRec = JSON.parse(body);
let movies = dataRec.data;
let totPages = dataRec.total_pages;
let sortArray = [];
movies.map((a) => {
sortArray.push(a.Title)
})
for (let i = 2; i <= totPages; i++) {
let newPage = i;
let url1 = 'https://jsonmock.hackerrank.com/api/movies/search/?Title=' + substr + "&page=" + newPage;
https.get(url1, (res) => {
res.setEncoding('utf8');
res.on('data', function(body) {
let newData = JSON.parse(body);
let newMovies = newData.data;
for (let i = 0; i < newMovies.length; i++) {
sortArray.push(newMovies[i].Title);
}
// console.log(JSON.stringify(sortArray.sort()));
sortArray.sort().forEach(title => console.log(title));
})
})
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment