What does the
var brackets = {
'(': ')',
'{': '}',
'[': ']'
};
From the following code do? Can you give examples of this kind of object in use? I know that objects can have methods and properties, but what does this mapping of brackets to opposite ones mean?
// Use an object to map sets of brackets to their opposites
var brackets = {
'(': ')',
'{': '}',
'[': ']'
};
// On each input string, process it using the balance checker
module.exports = function (string) {
var stack = [];
// Process every character on input
for (var i = 0; i < string.length; i++) {
if (brackets[stack[stack.length - 1]] === string[i]) {
stack.pop();
} else {
stack.push(string[i]);
}
}
return !stack.length;
};