Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/es5-shim/2.0.8/es5-shim.min.js"></script>
  <script src="https://esnext.github.io/js/esprima.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  # of literals: <span id="literal-count"></span> (should be 2)
</body>
</html>
 
/**
 * This exercise was taken from "What is an AST?",
 * the first in a series of blog posts on how esnext
 * works. See https://esnext.github.io/2014/07/31/what-is-an-ast/.
 */
function visit(ast, callback) {
  // TODO: Implement me.
}
function literalCount(ast) {
  var count = 0;
  visit(ast, function(node) {
    // Called once for every node in the AST.
    if (node.type === 'Literal') {
      count++;
    }
  });
  return count;
}
var ast = esprima.parse("var a = [1, 'n'];");
document.getElementById('literal-count').textContent = literalCount(ast);
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
eventualbuddhapro
0viewers