forked from HackYourFuture/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathex5-vscDebug.js
More file actions
47 lines (39 loc) · 1.35 KB
/
ex5-vscDebug.js
File metadata and controls
47 lines (39 loc) · 1.35 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
37
38
39
40
41
42
43
44
45
46
47
/*------------------------------------------------------------------------------
Full description at: https://github.com/HackYourFuture/Assignments/blob/main/3-UsingAPIs/Week2/README.md#exercise-5-using-the-vscode-debugger
Use the VSCode Debugger to fix the bugs
--------------------------------------------------------------- --------------*/
async function getData(url) {
const response = await fetch(url);
return response.json();
}
function renderLaureate({ knownName, birth, death }) {
console.log(`\nName: ${knownName.en}`);
if (birth) {
console.log(`Birth: ${birth?.date || 'Unknown'}, ${birth?.place?.locationString || 'Unknown'}`);
} else {
console.log(`Birth: Unknown`);
}
if (death) {
console.log(`Death: ${death?.date || 'Unknown'}, ${death?.place?.locationString || 'Unknown'}`);
} else {
console.log(`Death: still alive`);
}
}
function renderLaureates(laureates) {
laureates.forEach(renderLaureate);
}
async function fetchAndRender() {
try {
const data = await getData(
'http://api.nobelprize.org/2.0/laureates?birthCountry=Netherlands&format=json&csvLang=en'
);
if (Array.isArray(data.laureates)) {
renderLaureates(data.laureates);
} else {
console.error('No laureates found.');
}
} catch (err) {
console.error(`Something went wrong: ${err.message}`);
}
}
fetchAndRender();