The challenge is to write a function that evaluates whether an ordered list of edge characters — parentheses, brackets, or curly braces — is correctly balanced according to the following rules:
-
Each opening character must be eventually followed by its corresponding closing character. (e.g. an opening parenthesis must be followed by a closing parenthesis, but not necessarily immediately.)
-
The characters must be nested correctly, so that each pair of characters maintains a valid scope. (e.g. an opening bracket cannot be immediately followed by a closing curly brace.)
This challenge must be written in JavaScript. For ease of use, I suggest using node.
Don't freak out! Node, at its core, is simply a way to run JavaScript outside of your browser. You will not need any additional modules or libraries to complete this challenge. You can install Node using homebrew like this:
$ brew install node
Once you've done, that you can run JavaScript files from the command line. To access arguments passed in, you can use the built in argv functionality:
var some_input = process.argv
console.log() will, unsurprisingly, output to the console.
Your program should take a string of edge characters as an input and returns a boolean value. Your program should run like this:
$ node balanceValidator.js '[{}]'
true
$ node balanceValidator.js '{()'
false
$ node balanceValidator.js '([]{})()'
true
$ node balanceValidator.js '({)}'
false