Question

In: Computer Science

If it needs more information be specific. JavaScript Functions Create the makeBranches() function that will be...

If it needs more information be specific.

JavaScript Functions

Create the makeBranches() function that will be used to append node branches to the node tree diagram. The function will have two parameters named treeNode and nestedList. The treeNode parameter stores the current node from the source article and the nestedList parameter stores the structure of the node tree displayed in the web page. Add the commands described in the steps below.

Each time the makeBranches() function is called, it is because a new node has been discovered in the source article. Increase the value of the nodeCount variable by 1.

Create the following list item HTML fragment, storing the list item element in the liElem variable and the span element node in the spanElem variable.

 
  • +--

Append the spanElement node to the liElem element node; append the liElem node to the nestedList element node.

If treeNode represents an element node, then do the following:

  1. Increase the value of the elemCount variable by 1.
  2. Add the class attribute to the spanElem node, setting its value to “elementNode”.
  3. Append the text string “” to the spanElem node, where element is the name of the HTML element. (Hint: Use the textContent property to append the text of the element tag and not the element tag itself.)

Else if treeNode represents a text node, do the following:

  1. Increase the value of the textCount variable by 1.
  2. Declare the variable textString equal to the value of the text node.
  3. Jorge has provided a function named isWhiteSpaceNode() to determine whether a text node represents white space or not. Call the isWhiteSpaceNode() function using textString as the parameter value. If the function returns the value true, then do the following:
    1. Increase the value of the wsCount variable by 1
    2. Change the class attribute of the spanElem node to “whiteSpaceNode"
    3. Append the text string “#text” to the spanElem node
  4. If the isWhiteSpaceNode() function returns the value false, then do the following:
    1. Change the class attribute of the spanElem node to “textNode”
    2. Append the text string “text” to the spanElem node where text is the value of the textString variable.

The makeBranches() function should recursively move through the different levels of nodes in the source article. To apply recursion, test whether the number of child nodes of treeNode is greater than zero. If it is, then do the following:

  1. Create the following HTML fragment as an element node with the name newList:
     
    1. |
  2. Append newList to the nestedList element node.

  3. Loop through the child nodes of treeNode using n to represent each child node, and for each child node to call the makeBranches() function using n and newList as the parameter values.

Return to the makeTree() function and add commands to display the total count of nodes, element nodes, text nodes, and whitespace nodes within the span elements whose ids are “totalNodes”, “elemNodes”, “textNodes”, and “wsNodes”, respectively.

Document your work with informative comments throughout the JavaScript file.

Code

"use strict";

//global variables declaration

var nodeCount=0;

var elemCount=0;

var textCount=0;

var wsCount=0;

//event to run fuction

window.onload=makeTree;

  

function makeTree() {

  

  

}

function makeBranches(treeNode, nestedList) {

  

}

   

function isWhiteSpaceNode(tString) {

   return !(/[^\t\n\r ]/.test(tString));

}

Solutions

Expert Solution

Let’s demonstrate using an example. We’ll add a message on the page that looks nicer than alert.

Here’s how it will look:

<style>
.alert {
  padding: 15px;
  border: 1px solid #d6e9c6;
  border-radius: 4px;
  color: #3c763d;
  background-color: #dff0d8;
}
</style>

<div class="alert">
  <strong>Hi there!</strong> You've read an important message.
</div>

To create DOM nodes, there are two methods:

document.createElement(tag)

Creates a new element node with the given tag:

let div = document.createElement('div');

document.createTextNode(text)

Creates a new text node with the given text:

let textNode = document.createTextNode('Here I am');

Most of the time we need to create element nodes, such as the div for the message.

Creating the message

Creating the message div takes 3 steps:

// 1. Create <div> element
let div = document.createElement('div');

// 2. Set its class to "alert"
div.className = "alert";

// 3. Fill it with the content
div.innerHTML = "<strong>Hi there!</strong> You've read an important message.";

Related Solutions

Create a JavaScript function that will collect the information from the form and verify that it...
Create a JavaScript function that will collect the information from the form and verify that it is the correct type and that there are no blank textboxes. Save and test the file to ensure that the textbox information is collected and the script is working correctly. Use the onclick event within the submit button to call this function. Output validation error messages by writing directly to the with the id of "results." (You may want to use alert boxes for...
Making a blackjack game in javascript Create a function called createDeck() that will create an array...
Making a blackjack game in javascript Create a function called createDeck() that will create an array of objects (52). Each object contains two properties: suit and value. The suit property will be either 'Hearts', 'Clubs', 'Diamonds', or 'Spades'. The value property will be either 'Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three' , or 'Two''. Note: We will probably want to store possible suits and values in separate arrays. Create a function called shuffleDeck() that will...
Companies use demographic information to target specific groups of people to create a more meaningful piece...
Companies use demographic information to target specific groups of people to create a more meaningful piece of the market in which they can supply. In business, making this decision to target some while not targeting others can be difficult since expansion could be limited based on that specific groups’ dynamics. Select a characteristic that business use to segment the population and explain how that can benefit the company’s sales. Explain how targeting one group is not considered discrimination and how...
Write a javascript program according to the follow requirements: Create a function that converts Fahrenheit to...
Write a javascript program according to the follow requirements: Create a function that converts Fahrenheit to Celsius. It takes a single argument which represents degrees in Fahrenheit. It converts it and returns the degrees in Celsius. Create another function that converts Celsius to Fahrenheit. It takes a argument in Celsius and returns the degrees in Fahrenheit. Implement the function convert(isFtoC, from, to) below. It takes the following three arguments: isFtoC: a boolean that is true if degrees must be converted...
[Javascript] Create a function that gets the total amount and percentage of covid case data for...
[Javascript] Create a function that gets the total amount and percentage of covid case data for each Age Group in the covid cases data set(which contains the property "Age Group" in each case array). Age Groups: 'younger than 18', 'between 19 to 28 Years', 'between 29 to 38 Years','between 39 to 48 Years','between 49 to 58 Years','between 59 to 68 Years','between 69 to 78 Years','between 79 to 88 Years', and'older than 89' the function getSummaryOfAges(data) should return an Object with...
This needs to be written in JavaScript with two files. One HTML file and a JavaScript...
This needs to be written in JavaScript with two files. One HTML file and a JavaScript file. Make an empty HTML file, put three buttons inside the body and an empty main tag below the buttons. The text of the buttons should be "Foo", "Bar", and "FooBar" respectively. Don't put the " in the buttons that's just for clarification in these instructions. Add unique IDs to each button In your JavaScript, get 3 separate references to the buttons using the...
Write the following functions. Each function needs function comments that describes function and its parameters double...
Write the following functions. Each function needs function comments that describes function and its parameters double sphereVolume( double radius) double sphereSuface( double radius) double cylinderVolume( double radius, double height) double coneSurface( double radius, double height) double coneVolume( double radius, double height) That computes the volume and surface of the sphere with radius, a cylinder with a circular base with radius radius , and height height , and a cone with a circular base with radius radius , and height height...
Use PHP, javascript and a little bit of Html More focus on php Create a logout...
Use PHP, javascript and a little bit of Html More focus on php Create a logout page (for patients) in hotel management. Please provide the screenshot of the output too (logout page). Hint: the logout page should include like this thanks for visting etcetera. Thanks in advance
Write a javascript code to Create a function called Hotel that takes Room no, Customer name....
Write a javascript code to Create a function called Hotel that takes Room no, Customer name. amount paid. Write a code to call hotel function for each customer and display details of customers lodging in rooms with even room numbers. I need only js and html code. no css pls take screenshot of output , else I might dislike thanks
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts...
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts a Date object dateStr and returns a string that is the day of the week in English form (i.e. “Sunday”, “Monday”, etc.). Test your function by creating a date object that is a significant date to you (such as your birthday) and passing that date object to your function. Test your function at least twice with two different dates. Submit the dayOfWeek.js file to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT