Skip to content

Commit

Permalink
Complete balancedParens.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Dec 27, 2014
1 parent e879efc commit 60aedbc
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions balancedParens/balancedParens.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,35 @@
*/

var balancedParens = function (input) {
var stack = [];
var map = {
')' : '(',
']' : '[',
'}' : '{'
};
var length = input.length;
var i, currentChar, popped;

for (i = 0; i < length; i += 1) {
currentChar = input[i];

if ( isOpening(currentChar) ) {
stack.push( currentChar );
} else if ( isClosing(currentChar) ) {
popped = stack.pop();
if ( map[currentChar] !== popped ) {
return false;
}
}
}

return stack.length === 0;
};

var isOpening = function (char) {
return char === '(' || char === '[' || char === '{';
};

var isClosing = function (char) {
return char === ')' || char === ']' || char === '}';
};

0 comments on commit 60aedbc

Please sign in to comment.