I am quite new to JavaScript and I have a task to finish a certain app, in the process I have the following requirement:
Create a fetchBill function. It should assign (link I have given in the script) to an api variable. It should then use the browser's fetch function to make a HTTP request to api. Using an arrow function in a .then call to the fetch function, return the response after converting it to JSON. Using an arrow function in another .then call to the first one, take the converted JSON data in a data parameter and call displayCartTota lwith it. Make sure to handle errors that may occur, e.g by showing a warning message in the console.
Call the fetchBill function from inside startApp.
What I have tried:
I have written the following code, but I keep receiving a warning that I haven't declared the fechBill function as required and assigned a correct URL. And that I should check instructions!
Create a fetchBill function. It should assign (link I have given in the script) to an api variable. It should then use the browser's fetch function to make a HTTP request to api. Using an arrow function in a .then call to the fetch function, return the response after converting it to JSON. Using an arrow function in another .then call to the first one, take the converted JSON data in a data parameter and call displayCartTota lwith it. Make sure to handle errors that may occur, e.g by showing a warning message in the console.
Call the fetchBill function from inside startApp.
What I have tried:
I have written the following code, but I keep receiving a warning that I haven't declared the fechBill function as required and assigned a correct URL. And that I should check instructions!
Code:
<script>
"use strict";
const startApp = () => {
};
const displayCartTotal = ({results}) => results;
const fetchBill = () => {
const api = 'https://randomapi.com/006b08a801d82d0c9824dcfdfdfa3b3c';
fetch(api)
.then(res => {
res = JSON.stringfy(res);
return res.json()
})
.then(data => displayCartTotal(data))
.catch(err => consoole.log(err))
}
</script>
Comment