I suggest to make the following changes in your code:
- Initialize variable i only once.
There are two ways to do that. You may move initialization to top variable declaration block. In this case your for loop starts looking like this:
var i = 0,
...
for(; i < n; i++) { // Loop initialization part is empty! (Some may confused)
...
}
You may rewrite it to 'while' loop.
while(i < n) {
...
i++;
}
But there may be issue with manually hoisting variables declaration outside of loop. You cannot copy/paste loop in another place within you code base (you may need it in some cases) whitout issues (if you use strict mode, you'll get ReferenceError, because variable i may not be declared and initialized). In this case you need to find all variable declarations related to loop (i, n) and paste them too.
I suggest to move loop variable declaration logic to initialization part of for loop. In this case all loop variables are closely related to each other (high cohesion) and you may safely move this loop to any place within your code base.
- Better check input parameter s.
As i understand, balanced_parenthesis fuction should work with strings as input parameters. If this function is public API function, we need to check input parameters properly and fail fast.
You may add additional check for string type of input parameter. This check is not only save us from array-like objects and arrays (because they have length property), but also allows us to use loose equal operator (two equal signs) inside our loop (yes, we can safely use loose equal operator because we are sure that we are working with string).
- Use ternary operator in return statement.
Some prefer to use ternary operator for simple if/else cases due to it's readability. Also we may harness implicit coercion feature of JS for startCnt variable.
My solution is also imperfect, so i'll appreciate if you say what's wrong with my solution. Thanks.
P.S. Please, be patient to my English. It's also imperfect:)
function balanced_parenthesis(s) { if(s == null || typeof s != 'string') return null; var startCnt = 0; for(var i = 0, n = s.length; i < n; i++) { if(s[i] == "(") { startCnt += 1; } else if(s[i] == ")") { startCnt -= 1; if(startCnt < 0) { break; } } } return startCnt ? 'Unbalanced' : 'Balanced'; }
- function balanced_parenthesis(s) {
if(s == null) return null;- if(s == null || typeof s != 'string') return null;
var i = 0,startCnt = 0,n = s.length;- var startCnt = 0;
for(i = 0; i < n; i++) {if(s[i] === "(") {- for(var i = 0, n = s.length; i < n; i++) {
- if(s[i] == "(") {
- startCnt += 1;
} else if(s[i] === ")") {- } else if(s[i] == ")") {
- startCnt -= 1;
- if(startCnt < 0) {
- break;
- }
- }
- }
if(startCnt !== 0) {return 'Unbalanced';} else {return 'Balanced';}- return startCnt ? 'Unbalanced' : 'Balanced';
- }
describe("Balanced Parenthesis Tests", function(){ it ("should return Balanced", function(){ Test.assertEquals(balanced_parenthesis("(()())"), "Balanced"); }); it ("should return Unbalanced", function(){ Test.assertEquals(balanced_parenthesis("(())())"), "Unbalanced"); }); it ("should return Unbalanced", function(){ Test.assertEquals(balanced_parenthesis(")()("), "Unbalanced"); }); it ("should return null for null input", function(){ Test.assertEquals(balanced_parenthesis(), null); }); it ("should return null for array-like object", function(){ Test.assertEquals(balanced_parenthesis(["(", ")"]), null); }); it ("should return null for array-like object", function(){ Test.assertEquals(balanced_parenthesis({ length: 1 }), null); }); });
- describe("Balanced Parenthesis Tests", function(){
- it ("should return Balanced", function(){
- Test.assertEquals(balanced_parenthesis("(()())"), "Balanced");
- });
- it ("should return Unbalanced", function(){
- Test.assertEquals(balanced_parenthesis("(())())"), "Unbalanced");
- });
- it ("should return Unbalanced", function(){
- Test.assertEquals(balanced_parenthesis(")()("), "Unbalanced");
- });
- it ("should return null for null input", function(){
- Test.assertEquals(balanced_parenthesis(), null);
- });
- it ("should return null for array-like object", function(){
- Test.assertEquals(balanced_parenthesis(["(", ")"]), null);
- });
- it ("should return null for array-like object", function(){
- Test.assertEquals(balanced_parenthesis({ length: 1 }), null);
- });
- });