Balancing longest balanced sequence array with javascript
How to balance and array the code like input 1,2,-2,-1,3,4,-3 the output is 4 because 1,2,-2,-1 is balance one rather than 3,4,-3 You must open parentheses must be closed by the same type of parentheses. this is my code : var ans, s, temp, n, a; n = 7; a = 1,2,-2,-1,3,4,-3; s = []; ans = 0; for (var i = 0, a = n; i < a.length; i += 1) { if (a[i] > 0) { s.append(i); } else { if (a[i] < 0) { if (s && a[i] === -a[s.slice(-1)[0]]) { s.pop(); if (!s) { temp = -1; } else { temp = s.slice(-1)[0]; } ans = max(ans, i - temp); } else { s.append(i); } } } } console.log(ans); please teach me with hard ways not simple ways if can Thank You