<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/mocha/1.13.0/mocha.min.css">
<script src="http://chaijs.com/chai.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/mocha/1.13.0/mocha.min.js"></script>
<div id="mocha"></div>
<p>Usually optimization involves <a href="http://blog.moertel.com/posts/2013-05-11-recursive-to-iterative.html">taking a recursive function and making it iterative</a>. To get used to using mocha, do the reverse. Take the, "walkIterative" function and make it recursive while it keeps the order of elements walked and callbacks fired.</p>
<p>Prove that it works using mocha tests. Feel free to add more than one test to the "walkRecursive" describe block. Use the tests as your dev and debugging crutches. An empty "walkRecursive" function and describe block has been setup for you.</p>
<div id="test">1
<div>2</div>3
<div>4
<div>5
<div>6
<div>7</div>8
</div>9
<div>10</div>11
</div>12
<div>13</div>14
<div>15</div>16
</div>17
</div>
JavaScript
describe('dom walking', function() {
beforeEach(function() {
text = [];
testEllie = document.getElementById("test");
});
describe('by iteration', function() {
it('should encounter text nodes in order', function() {
walkIterative(testEllie,report);
text.should.deep.equal(['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17']);
});
});
describe('by recursion', function() {
});
});
function report(depth) {
var value;
if (TEXT_NODE === this.nodeType) {
if (value = this.nodeValue.match(/\d+/)) {
text.push(value[0]);
}
}
}
function walkRecursive(node, callback) {
}
// https://gist.github.com/cowboy/958000
function walkIterative(node, callback) {
var skip, tmp;
var depth = 0;
do {
if ( !skip ) {
skip = callback.call(node, depth) === false;
}
if ( !skip && (tmp = node.firstChild) ) {
depth++;
} else if ( tmp = node.nextSibling ) {
skip = false;
} else {
tmp = node.parentNode;
depth--;
skip = true;
}
node = tmp;
} while ( depth > 0 );
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.