Help with code I am currently working on some code in JSFiddle.net but nothing i
ID: 3750825 • Letter: H
Question
Help with code
I am currently working on some code in JSFiddle.net but nothing is displaying and no errors are coming up when I debug. Also, I set parameters to correct No-Library (pure JS) which is what was recommended by user who made code. Any help would be greatly appreciated.
HTML
<input type="textbox" id="v" value="Node 1" />
<input type="button" value="Add Node" />
<div id="output">
Javascript
function LinkedList() {
this.head = null;
this.tail = null;
this.length = 0;
}
function Node() {
this.next = null;
this.prev = null;
this.content = null;
}
LinkedList.prototype.add = function(_content) {
var node = new Node(); node.content = _content;
if (this.head == null) {
this.head = node; this.length = 1;
return node;
}
if (this.tail == null) {
this.tail = node;
this.tail.prev = this.head;
this.head.next = this.tail;
this.length = 2;
return node;
}
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
this.length++;
return node;
}
LinkedList.prototype.print = function() {
if (this.head == null) return "Empty List";
var s = "";
var node = this.head;
while (node != null) {
s += node.content + " ";
node = node.next;
}
return s;
}
var aList = new LinkedList();
function addNode() {
var c = document.getElementbyId("v").value;
aList.add(c);
document.getElementById("output").innerHTML = aList.print();
}
Explanation / Answer
Please try the below code and if this is not working please revert back with little more details like what is expected out of this code.
<HTML><title>My Page</title><body>
<input type="textbox" id="v" value="Node 1" />
<input type="button" value="Add Node" />
<div id="output">
</div>
<script type="text/javascript">
function LinkedList() {
this.head = null;
this.tail = null;
this.length = 0;
}
function Node() {
this.next = null;
this.prev = null;
this.content = null;
}
LinkedList.prototype.add = function(_content) {
var node = new Node(); node.content = _content;
if (this.head == null) {
this.head = node; this.length = 1;
return node;
}
if (this.tail == null) {
this.tail = node;
this.tail.prev = this.head;
this.head.next = this.tail;
this.length = 2;
return node;
}
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
this.length++;
return node;
}
LinkedList.prototype.print = function() {
if (this.head == null) return "Empty List";
var s = "";
var node = this.head;
while (node != null) {
s += node.content + " ";
node = node.next;
}
return s;
}
var aList = new LinkedList();
function addNode() {
var c = document.getElementbyId("v").value;
aList.add(c);
document.getElementById("output").innerHTML = aList.print();
}
</script>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.