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 https://randomapi.com/api/006b08a801d82d0c9824dcfdfdfa3b3c 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 displayCartTotalwith 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!
The following are further instructions I have been given:
Write all Javascript strictly with ES6. This means use arrow functions instead of the function keyword. Declare variables and functions with const or let. Use const by default, and only use let if you are sure you need to re-assign values to the said variable. Use the Selector API instead of the getElementBy... or getElementsBy... APIs. See https://developer.mozilla.org/en-US/docs/Web/API/Document_object_model/Locating_DOM_elements_using_selectors
Install the JSON Viewer Chrome extension (https://chrome.google.com/webstore/detail/jsonview/chklaanhfefbnpoihckbnefhakgolnmc?hl=en), open a new tab and go to this URL (https://randomapi.com/api/006b08a801d82d0c9824dcfdfdfa3b3c). You will be making HTTP requests to that API endpoint, so spend some time looking at the structure of the response data.
"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))
}