1

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);
2
  • 1
    Not so infamous, I'm having trouble looking up what the "braces problem" is in the first place. Is it just trying to parse through braces? Commented Apr 29, 2014 at 1:00
  • Checking if the expression has matching braces. If it does I return 1 if not I return 0 Commented Apr 29, 2014 at 1:01

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

@Geo seemed fun to make this, made my own: jsfiddle.net/Pe53Q/1 . Feel free to check it out
Very interesting approach Juvian :)
1

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.

3 Comments

But I am calling the Stack() from inside the braces() right after the first else
But the Stack object doesn't have any relation to push(), pop(), peek(), length(), and clear() functions. So you'll need to nest those functions inside the Stack object in order to use them properly.
@LaughDonor I made the changes but I have the same issue. If that was the issue, would't it be affecting my first expression and not only the second one?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.