Skip to content

Instantly share code, notes, and snippets.

@raorao
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raorao/9594680 to your computer and use it in GitHub Desktop.
Save raorao/9594680 to your computer and use it in GitHub Desktop.

BalanceValidator

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:

  1. 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.)

  2. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment