Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

if ( document . readyState = = = 'loading' ) { document.addEventListener ( ' DOMContentLoaded ' , readJSONFile ) } else { readJSONFile ( )

if(document.readyState === 'loading'){
document.addEventListener('DOMContentLoaded', readJSONFile )
} else {
readJSONFile();
}
function readJSONFile(Filename, callback){
fetch(Filename)
.then(response => response.json())
.then(data => callback(data))
.catch(error => console.error('Error reading the JSON file', error));
}
function booksObject(data){
const tableBody = document.querySelector('tbody');
createTableRows(data, tableBody);
}
function createTableRows(books, tableBody){
const fragment = document.createDocumentFragment();
for (const isbn in books){
const book = books[isbn];
const row = document.createElement('tr');
const isbnCell = document.createElement('td');
const titleCell = document.createElement('td');
const priceCell = document.createElement('td');
const quantityCell = document.createElement('td');
const totalCell = document.createElement('td');
isbnCell.textContent = isbn;
titleCell.textContent = book.title;
priceCell.textContent = book.price;
quantityCell.textContent = book.quantity;
totalCell.textContent = book.price * book.quantity;
row.appendChild(isbnCell);
row.appendChild(titleCell);
row.appendChild(priceCell);
row.appendChild(quantityCell);
row.appendChild(totalCell);
fragment.appendChild(row);
}
tableBody.appendChild(fragment);
}
readJSONFile('data/books.json', booksObject);
function booksObject(){
let books ={
9781118876138 : {
title : "DATA SCIENCE & BIG DATA ANALYSIS",
price : 60.24,
quantity : 1},
9781107186125 : {
title : "PRINCIPLES OF DATABASE MANAGEMENT",
price : 77.25,
quantity : 1},
9781587205880 : {
title : "CCNA ROUTING & SWITCHING PORTABLE COMMAND GUIDE",
price : 24.26,
quantity : 2},
9781119288312 : {
title : "CCNA ROUTING AND SWITCHING : EXAM",
price : 73.50,
quantity : 2},
9781305078628 : {
title : "HANDS-ON MICROSOFT WINDOWS SERVER 2016",
price : 108.75,
quantity : 1},
9781541895386 : {
title : "INTRODUCTION TO WEB DEVE",
price : 65.25,
quantity : 1},
9781943872381 : {
title : "MURACH'S PHP AND MYSQL",
price : 43.25,
quantity : 1},
9780134167008 : {
title : "REVEL FOR LIANG JAVA ACCESS CARD",
price : 86.75,
quantity : 1}
}}
Error I am getting is "failed to fetch at readJSONFile. How do i fix this?
Also any corrections on code will help too. THANK YOU

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Google Analytics 4 The Data Driven Marketing Revolution

Authors: Galen Poll

2024th Edition

B0CRK92F5F, 979-8873956234

More Books

Students also viewed these Databases questions