Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Javascript we are going to create a Class called Chain. If you need a little

ID: 3674121 • Letter: I

Question

In Javascript we are going to create a Class called Chain. If you need a little primer on creating objects and classes in Javascript see http://www.w3schools.com/js/js_object_definition.asp - if you know your object oriented code this will be relatively straightforward. Your Chain will be made up of Links. Each link will have an id, a value (String) and a pointer to the next link ( NextLink ) - which is simply a pointer to the id of the next link in the Chain. Please note that this is a very inefficient representation of the Chain. Here is a simple Javascript code definition of a Link for you to use. https://jsfiddle.net/reaglin/q46obht6/ You will make a Chain consisting of 5 links. You will also create a print function for the chain that will print the links in link order. You may need to create some functions to support this functionality.

Explanation / Answer

html code to run script:

This is a demonstration of the concept of making a Linked List (in this case we call it a chain in JavaScript). There are 2 object involved here - and this is a very easy conceptual way to do this.
<br/>
<br/> Add Link Name:
<input type="textbox" id="LinkName" Value="New Link" />
<input type="button" id="AddLink" value="Add Link" />
<input type="button" id="DisplayHead" value="Display Chain Head" />
<p id="demo"></p>

javascript code:

var chain = new List();

function addLink() {
var newLinkName = document.getElementById("LinkName").value;
alert("Adding a Link Named: " + newLinkName);
chain=chain.add(newLinkName);
}

function __display()
{
chain.print();
}

function Node(data) {
this.data = data;
this.next = null;
}


function List() {
this._length = 0;
this.head = null;
}


List.prototype.add = function(value) {
var node = new Node(value);
var currentNode = this.head;
// 1st use-case: an empty list
if (!currentNode) {
this.head = node;
this._length++;
document.getElementById("demo").innerHTML = currentNode.data
return this;
}

// 2nd use-case: a non-empty list
while (currentNode.next) {
currentNode = currentNode.next;
}

currentNode.next = node;

this._length++;

return this;
};

List.prototype.print = function() {
var currentNode=this.head;
while (currentNode.next) {
document.getElementById("demo").innerHTML = currentNode.data+" ";
currentNode = currentNode.next;
}
document.getElementById("demo").innerHTML = currentNode.data+" ";
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote