JSFiddle - React, Tailwind, and code Playground
HTML
<p>Check out <a href="http://stackoverflow.com/q/20986255/534862" target="_blank">the original question</a> for more info<br>This answer is explained at <a href="http://stackoverflow.com/a/21024204/534862" target="_blank">this post</a>
<form action="/action_page.php">
Enter your name:
<input name="firstname" type="text">
<br><br>
<input type="submit">
</form>
</p>
<p>The following is the example output from the pattern<br><code>((1 or 3) and (2 or 4) or 5)</code></p>
CSS
body{
font-family: arial,sans-serif;
}
code{
background: #eee;
}
pre{
background: #eee;
}
JavaScript
(function CalcJS() {
/**
* This is a source example of my original question on
* http://stackoverflow.com/questions/20986255/converting-conditional-equation-from-infix-to-prefix-notation
*
* This is my solution and use it at your own risk
* @author Lionel Chan <chaoszcat[at]gmail.com>
*/
/**
* isNumeric, from jQuery. Duplicated here to make this js code pure
* @param {mix} n Test subject
* @returns {boolean} true if it's numeric
*/
function isNumeric(n) {
return !isNaN(parseFloat(n))&&isFinite(n);
}
/**
* Node class - represent a operator or numeric node
* @param {string} token The token string, operator "and", "or", or numeric value
*/
function Node(token) {
this.parent = null;
this.children = []; //one node has two children at most
this.token = token;
this.is_operator = token === 'and' || token === 'or';
this.is_numeric = !this.is_operator;
this.destroyed = false;
}
Node.prototype = {
isOperator: function() { return this.is_operator;},
isNumeric: function() { return this.is_numeric;},
//While building tree, a node is full if there are two children
isFull: function() {
return this.children.length >= 2;
},
addChild: function(node) {
node.parent = this;
this.children.push(node);
},
hasParent: function() {
return this.parent !== null;
},
indexOfChild: function(node) {
for (var i = 0 ; i < this.children.length ; ++i) {
if (this.children[i] === node) {
return i;
}
}
return -1;
},
removeChild: function(node) {
var idx = this.indexOfChild(node);
if (idx >= 0) {
this.children[idx].parent = null; //remove parent relationship
this.children.splice(idx, 1); //splice it out
}
},
/**
* Pass my children to the target node, and destroy myself
*
* @param {Node} node A target node
*/
passChildrenTo: function(node) {
for (var i = 0 ; i < this.children.length ; ++i)...