Skip to content

Instantly share code, notes, and snippets.

@seemantk
Last active January 20, 2016 05:26
Show Gist options
  • Save seemantk/350f484412f8c27db55c to your computer and use it in GitHub Desktop.
Save seemantk/350f484412f8c27db55c to your computer and use it in GitHub Desktop.
balance checker
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
svg { width:100%; height: 100% }
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see!
var inputs = ['{}()[]', '{(})'];
function check(v) {
var scores = {}
, symbols = {
'square': ['[', ']']
, 'curly': ['{', '}']
, 'paren': ['(', ')']
}
;
d3.keys(symbols).forEach(function(s) {
scores[s] = v.indexOf(symbols[s][1]) - v.indexOf(symbols[s][0]);
})
return scores;
} // check()
var res = inputs.map(function(str) {
var blah = check(str);
return d3.values(blah).every(function(v) { return v%2; }) ? "YES" : "NO";
});
console.log(res);
return res;
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment