Skip to content

Instantly share code, notes, and snippets.

@aspencer8111
Last active December 12, 2017 22:19
Show Gist options
  • Save aspencer8111/5d09f70c8e2d72a88b6fd70968e6b3f0 to your computer and use it in GitHub Desktop.
Save aspencer8111/5d09f70c8e2d72a88b6fd70968e6b3f0 to your computer and use it in GitHub Desktop.
HN Clone
```javascript
let url = 'https://newsapi.org/v2/top-headlines?sources=hacker-news&apiKey=<YOUR API KEY GOES HERE>'

fetch(url)
  .then((r) => { return r.json() })
  .then(
    (data) => {
      let results = data.articles // get the array of results from the data object
      let articlesList = document.createElement('ul') // create a new `<li>`
      let body = document.querySelector('body') // attach to the `<body>` node of the DOM
      body.appendChild(articlesList) // append the list to the body
      results.map( (article) => { // loop through each article object in the array of articles
          let article = document.createElement('li') // create a article item `<li>`
          article.innerHTML = '<a href="' + article.href + '">' + article.title + '</a>' // add the link to each li with each article's title as the text and link as the href
          articlesList.appendChild(article) // append each article to the `<ul>`
        }
      )
    }
  )
  .catch(
    (e) => { console.log(`An error occurred: ${e}`) }
  )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment