Please explain to me the meaning of these codes

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

DO NOT COPY FROM OTHER WEBSITES

 

Please explain to me the meaning of these
codes
function onSuccess(data) {
//console.log(data);
var books = JSON.parse(data);
document.getElementByld("bookList").append
Child(createTable(books));
}
var table_ = document.createElement('table'),
tr_ = document.createElement('tr'),
th_ = document.createElement('th'),
td_ = document.createElement('td');
function createTable(books) {
var table = table_.cloneNode(false),
columns = createTableHeader(books[0],
table);
Transcribed Image Text:Please explain to me the meaning of these codes function onSuccess(data) { //console.log(data); var books = JSON.parse(data); document.getElementByld("bookList").append Child(createTable(books)); } var table_ = document.createElement('table'), tr_ = document.createElement('tr'), th_ = document.createElement('th'), td_ = document.createElement('td'); function createTable(books) { var table = table_.cloneNode(false), columns = createTableHeader(books[0], table);
Expert Solution
Step 1 Explanation for the given code mention as below,

Note : I had re-factoring the given code , adding some additional functionality , with comments such that the code works.

Code with comments as explanation is mention as below:-

 

<html>
  <head>  
    <title>Re-factoring the code so that it works</title>
  </head>
  <body>
  <div id="bookList"></div>
</body>

<script>

// firstly  onSuccess function will call
function onSuccess()
{
  /*let's define json data (using harcoded json , but it may come from 
  any api as response and then, assign it to the constant variable named 
  as data*/

  const data = '{"0":"javascript", "1":"C", "2":"C++"}';
  var books = JSON.parse(data); //here parsing json data

  //returns an element object whose id is bookList
  var bookList = document.getElementById("bookList");
  
  /*function createTable invokes and return a string as response 
  and then update the content of a div element having id="bookList"*/
  bookList.innerHTML = createTable(books);
}

   /*creates the HTML element specified by tag mamed as table and 
   assign  it to the variable table_, similarly create html element
   for tr and assign it to variable tr_, similarly create html 
   element for th_ and assign it to variable th_ and similarly 
   create html element for td_ and assign it to variable td_*/
   

   var table_ = document.createElement('table'),
        tr_ = document.createElement('tr'),
        th_ = document.createElement('th'),
        td_ = document.createElement('td');

 

  function createTable(books)
  {
    /*cloneNode function generate a replica for a node,
    here it is set to false and asign it to variable named
    as table  and then invokes createTableHeader function
    and the response return by the invoked function will be 
    assigned to  insertColumn  variable and add table's 
    column value with content contained in zeroth index 
    of an array named as book (that conatins a strng named as
    javascript , i.e. books[0] contains value as javascript and 
    return the response to the invoked /called function*/
     

     var table = table_.cloneNode(false),
        columns = createTableHeader(books[0],table)
        var insertColumn = columns.insertCell(0);
        return insertColumn.innerHTML = books[0];
    }

 
    /*create a new function named as createTableHeader,
    note that this additional function was not mentioned
    on the above question shown , I had added this functionality
    for the code to work completely, it accepts two arguments first is 
    content consists in books[0]  and a tableInstance*/
   

    function createTableHeader(book, tableInstance)
    { 
      /*As, variable named as table_  is defined is globally 
      available and the using createTHead function , 
      that will generate an empty thead element and add 
      it to the table , assign the return object into the 
      variable named header*/
     
      var header = table_.createTHead();

      /*insertRow function generate an empty table row element and add it 
      to the first position of table head and assinging the return obejct to 
      a variable named row*/
     

      var row = header.insertRow(0);   

      //returns the object named as row to the invoked or called function 
      return row;
    }
    

    //calling  onSuccess() function
    onSuccess();
    </script>
</html>

 

 

 

 

 

 

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY