I'm currently working on a calculator project that involves evaluating expressions like (5+4)
. The approach I am taking is to pass the pressed buttons into an array and then create a parse tree based on the data in that array.
One issue I'm facing with my code is that it fails to push the value of the right parenthesis ")" into the array. Even when I tried moving calcArray.push()
outside the if statements, it still wouldn't add ")" to the array. Can someone provide some assistance?
$(document).ready(function(){
var calcArray = new Array();
$("input").click(function(){
var activeButton = this.value;
console.log(activeButton);
if(!isNaN(activeButton))
{
calcArray.push(parseInt(activeButton));
console.log(calcArray);
}
else if(activeButton === "=")
{
evaluate(buildTree(calcArray));
calcArray = [];
}
else
{
calcArray.push(activeButton);
}
});
});
The BuildTree function:
function BinaryTree(root) {
this.root = root;
this.activeNode = root;
}
function Node(element){
...
}
var buildTree = function(array)
{
...
}
var evaluate = function(node){
...
};