04-Dates

Here we'll work with having the option to include a beginning date and ending date in our search.

TWO CONDITIONALS

Let's add the conditional statements for end date and start date. Let's do this after the url and before the fetch. Note that some code has been omitted below. We are in the fetchResults function, if you're lost:

  url = baseURL + '?api-key=' + key + '&page=' + pageNumber + '&q=' + searchTerm.value;
//INSERT HERE  
  if(startDate.value !== '') {
      console.log(startDate.value)
    url += '&begin_date=' + startDate.value;
  };

  if(endDate.value !== '') {
    url += '&end_date=' + endDate.value;
  };
//END HERE
  fetch(url).then(function(result) {

If you print the url to the now, you should see this.

This is a rudimentary version of form validation, which is a method of ensuring that specific fields are filled out in a form. You've seen form validation. Think about when you go sign up for a new account somewhere, and you are prompted for more information. Form Validation will force you to enter information. In this case, we are not being forced to enter a start date or end date. It's optional. If the input fields for dates aren't blank, denoted by the !== '', the date values will be added to the URL string. If they're blank, the expressions inside of the conditionals are ignored.

More Options

There are many more options that you could use to append more items to your URL strings. If you're interested, go to the NYT API docs and play around.

Last updated