var key = 0;
var array = [];
var errors = [];
var result, total, x, y;
document.getElementById("click").onclick = function() {
myFormSubmit()
};
function myFormSubmit() {
array = [];
errors = []; //reset
x = document.getElementById("x-value").value;
y = document.getElementById("y-value").value;
try {
if (x == "") throw "You didn't enter a x value.";
if (isNaN(x)) throw "Enter a numeric value for x. You entered " + x;
x = parseInt(x);
} catch (err) {
errors.push(err);
}
try {
if (y == "") throw "You didn't enter a y value.";
if (isNaN(y)) throw "Enter a numeric value for y. You entered " + y;
y = parseInt(y);
} catch (err) {
errors.push(err);
}
document.getElementById('output').innerHTML = errors.join("<br>");
if (errors.length == 0) {
var tree = new BinarySearchTree();
tree.insert(3);
tree.insert("*");
tree.insert("(");
tree.insert(x);
tree.insert("+");
tree.insert(5);
tree.insert("*");
tree.insert(y);
tree.insert(")");
tree.preOrderTraverse(calculate_expression); //when called uses callback of preOrderTraverse to return nodes in order they were entered into tree (tracked by node.key, then stores them into an array. once complete, call the values from the array with the execption of the operators. Instead of using switch statement, as I did earlier this semester, to convert known operator value from string to operation, just place operator in palce as necessary.
}
}
function BinarySearchTree() {
var Node = function(key, value) {
this.key = key;
this.value = value;
this.left = null;
this.right = null;
};
var root = null;
this.insert = function(value) {
key++;
var newNode = new Node(key, value);
if (root === null) {
root = newNode;
} else {
insertNode(root, newNode);
}
};
//Pre-order Traversal
this.preOrderTraverse = function(callback) {
preOrderTraverseNode(root, callback);
};
}
var...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.