-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
36 lines (35 loc) · 1.5 KB
/
Copy pathmain.js
File metadata and controls
36 lines (35 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
async function getNews(){
//change the YOUR-API-TOKEN with your API token from the Washington post API website
await fetch('https://api.nytimes.com/svc/mostpopular/v2/viewed/1.json?api-key=YOUR-API-TOKEN')
.then(d => d.json())
.then(response => {
console.log(response.results);
for(var i = 0; i < response.results.length; i++){
const output = document.getElementById('output');
try{
output.innerHTML += `
<div class="card">
<div class="card-body">
<img src="${response.results[i]['media'][0]['media-metadata'][2].url}" class="card-img-top" alt="${response.results[i]['media'][0].caption}" title="${response.results[i]['media'][0].caption}"><br>
<h2 class="card-title">${response.results[i].title}</h2>
<div class="card-text">
<p>${response.results[i].abstract}</p>
</div>
</div>
</div>
<br>
<br>
`
console.log(response.results[i]['media'][0].caption);
}
catch(err){
console.log(err);
}
// you can comment the (only) following two lines
console.log(response.results[i].title);
console.log(i);
}
document.getElementById('copyright').innerHTML = response.copyright;
})
}
getNews();