diff --git a/fetch/programmer-humour/fetch.js b/fetch/programmer-humour/fetch.js new file mode 100644 index 00000000..4b0a297d --- /dev/null +++ b/fetch/programmer-humour/fetch.js @@ -0,0 +1,17 @@ +"use strict"; +const imgElem = document.querySelector("img"); +const errorMessage = document.querySelector("#errorMessage"); + +const fetchData = async () => { + try { + const response = await fetch("https://xkcd.now.sh/?comic=latest"); + const data = await response.json(); + return data; + } catch (error) { + errorMessage.style.display = `contents`; + errorMessage.textContent = error; + } +}; +fetchData().then((data) => { + imgElem.setAttribute("src", data.img); +}); diff --git a/fetch/programmer-humour/index.html b/fetch/programmer-humour/index.html new file mode 100644 index 00000000..5eced904 --- /dev/null +++ b/fetch/programmer-humour/index.html @@ -0,0 +1,14 @@ + + +
+ + +