s = ['[', ']', '{', '}', '(', ')'];
copied = s;
console.log(copied.length);
var output;
for (i = 0; i < s.length; i++) {
console.log('calling function', i);
different();
}
function different() {
if (copied[0] == '(' && copied[1] == ')') {
copied.splice(0, 2);
output = 'valid';
console.log('checking', copied);
} else if (copied[0] == '{' && copied[1] == '}') {
copied.splice(0, 2);
output = 'valid';
} else if (copied[0] == '[' && copied[1] == ']') {
copied.splice(0, 2);
output = 'valid';
console.log('checking', copied);
} else {
output = 'invalid';
}
}
console.log(copied);
console.log(copied.length);
console.log('result is ', output);
i am getting the following output
6
calling function 0
checking [ '{', '}', '(', ')' ]
calling function 1
[ '(', ')' ]
2
result is valid
The problem is for loop runs only twice but it is supposed to run 6 times.