PLEASE SEPARATE HTML AND JAVA CODING AND MUST WORK IN JSFIDDLE! Assignment 1 - I
ID: 668369 • Letter: P
Question
PLEASE SEPARATE HTML AND JAVA CODING AND MUST WORK IN JSFIDDLE!
Assignment 1 - 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
// Define the link object
function Link(_id, _value, _next) {
this.id = _id; // The id of the current link
this.value = _value; // The value stored
this.next = _next; // a pointer to the next link, this is 0 if it is the last link in the chain
}
Link.prototype.asString = function () {
return "Link: " + this.id +
" Value:" + this.value +
" Points To: " + this.next + "<br/>";
};
// Define the Chain object
function Chain(_firstValue) { // We will define the chain with the first link defined
this.length = 1;
// I like to keep track of the first link, not really necessary
this.head = new Link(1, _firstValue, 0);
// I need a place to store this links - an array will work fine and is pretty much one of the few options you have in JS
this.linkStorage = [];
// Oh yea - since we aren't creating an empty Chain - let's fill in that first value
this.linkStorage.push(this.head);
}
Chain.prototype.lastLink = function () {
// A function to find the last link in the chain
}
Chain.prototype.addLink = function (_linkValue) {
// A function to add a link to the chain
}
Chain.prototype.print = function () {
// You'll need this too, so we can see the output
}
// Let's try it by making a single link chain and testing it.
var chain = new Chain('Link 1');
var chain_1 = new Chain('Link 2');
var chain_2 = new Chain('Link 3');
var chain_3 = new Chain('Link 4');
var chain_4 = new Chain('Link 5');
// This should print out
document.getElementById("demo").innerHTML = chain.head.asString();
document.getElementById("demo_1").innerHTML = chain_1.head.asString();
document.getElementById("demo_2").innerHTML = chain_2.head.asString();
document.getElementById("demo_3").innerHTML = chain_3.head.asString();
document.getElementById("demo_4").innerHTML = chain_4.head.asString();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.