Question

I am working with stacks on the infamous 'Braces' problem and I got to a halt. This should be an easy fix but my eyes are not much of a help at the moment.

The first call to the function is working like a charm but the second one is running an extra time and I can't see why.

The first call returns 01110 which is correct but the second returns 011110 which is not...

If you can't read the code here, got to the fiddle

//constructor function for our Stack class
function Stack() {
    this.dataStore = [];
    this.top = 0;
    this.push = push;
    this.pop = pop;
    this.peek = peek;
    this.length = length;
    this.clear = clear;
}

function push(element) {
    this.dataStore[this.top++] = element;
}

function pop() {
    return this.dataStore[--this.top];
}

function peek() {
    return this.dataStore[this.top - 1];
}

function length() {
    return this.top;
}

function clear() {
    this.top = 0;
}


function braces(expression) {

    for (var i = 0; i < expression.length; i++) {
        //if the number of elements in the expression is odd, it is guaranteed not to have a matching expression
        //therefore we print 0
        if (expression[i].length%2 !== 0) {
            console.log(0);
        } else {
            var s = new Stack();
            var startPoint = expression[i].charAt(0);

            //if the expression starts with an open brace it means we will not have a matching expression so we print 0
            if (startPoint == '(' || startPoint == '{' || startPoint == '[') {
                for (var j = 0; j < expression[i].length; j++) {
                    var char = expression[i].charAt(j);
                    var h = '';
                    if (char == '(' || char == '{' || char == '[') {
                        s.push(char);
                    } else {
                        h = s.peek();
                        if (h == "(" && char == ")") {
                            s.pop();
                        } else if (h == "{" && char == "}") {
                            s.pop();
                        } else if (h == "[" && char == "]") {
                            s.pop();
                        }
                    }
                }
            } else {
                console.log(0);
            }

            if (s.length() == 0) {
                console.log(1)
            } else {
                console.log(0);
            }
        }
    }
}

var expr = [ "}()()", "[]({})", "([])", "{()[]}", "([)]" ]; //working
var expr2 = [ "}()(){", "[]({})", "([])", "{()[]}", "([)]" ]; //running an extra time

braces(expr);
Was it helpful?

Solution

Change this:

        else {
            console.log(0);
            continue; //this is new
        }

        if (s.length() == 0) {

Your function would log both 0 and 1/0 if the startpoint is not { or ( or [ and the length of s was 0

OTHER TIPS

Your stack functions are all outside of the scope of Stack() and therefore the data probably won't be what you expect. You can start fixing this by putting functions inside the Stack() function:

function Stack() {
    this.dataStore = [];
    this.top = 0;
    this.push = push;
    this.pop = pop;
    this.peek = peek;
    this.length = length;
    this.clear = clear;

    this.pop = function () {
        // pop
    }

    this.push = function () {
        // code
    }

    this.peek = function () {
        // code
    }

}

That way, the methods all have access to the same data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top