Easily search for NY Times articles.
This application is compatible with the most commonly used web browsers.
- Working Site
- Returned articles
- Search
//Call to build query. Sends query to another function that return the articles according to the query.
//Calls to render the articles
$("#search").on("click", function () {
let numberOfRecords = $("#searchFilter [name=numberOfRecords]").val().trim();
var queryURL = getQuery();
getArticles(queryURL).then(function (articles) {
renderArticles(articles, numberOfRecords);
});
});
- This listerner takes care of th esteps necessary to go from getting the user's information to rendering the articles on the user's web. Calls a functon to build the query. Then with the query it calls a function that will get the articles from the NYT api. Once the articles are ready, they are sent to another function that renders the articles to the HTML.
- Query building
//Build query given the users parameters
function getQuery() {
var queryURL = "https://api.nytimes.com/svc/search/v2/articlesearch.json?";
let terms = $("#searchFilter [name=term]").val().trim();
let startYear = $("#searchFilter [name=startYear]").val().trim();
let endYear = $("#searchFilter [name=endYear]").val().trim();
var queryParams = { "api-key": "3NYM77KW72ndmEaRDzr2cjaOcabhAIRW" };
queryParams.q = terms;
if (parseInt(startYear)) {
queryParams.begin_date = startYear + "0101";
}
if (parseInt(endYear)) {
queryParams.end_date = endYear + "0101";
}
return queryURL + $.param(queryParams);
}
- This function takes care of building the query. It gets the values from the fields that were filled form the user. These then add to an object of all the necesary parameters for the query. Then, the queryParams object is added to the queryURL with the $.param() function.
- 💼 Carlos Toledo: portfolio
Github: kqarlos
- LinkedIn: carlos-toledo415