Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.


JavaScript
"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))		
		}
Posted
Updated 25-Aug-19 15:52pm
v3
Comments
Richard Deeming 8-Aug-19 10:30am    
Call the fetchBill function from inside startApp.
You seem to have missed that bit.

.catch(err => consoole.log(err))
You've got too many "o"s in console there.

1 solution

Hey there here is how you'd do it

fetch(apiEndpoint).then(response => response.json())
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900