public
Created

Coding Dojo -- July 16, 2011

  • Download Gist
main.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
Hallway = function(){
/*
var private_property = null;
this.public_property = null;
this.public_method = function(){
private_method();
};
private_method = function(){
};
*/
var doors = [ 0, 0, 0, 0 ];
var trip_count = 0;
this.num_doors = doors.length;
this.state = function()
{
return doors;
}
this.trips = function(num_trips)
{
for (var crnt_trip = 0; crnt_trip < num_trips; crnt_trip++)
{
this.trip();
}
}
this.trip = function()
{
trip_count++;
doors.forEach(function(door,index)
{
if ((index + 1) % trip_count == 0){
if (doors[index])
doors[index] = 0;
else
doors[index] = 1;
}
/* if (!(door%trip_count))
{
} else {
doors[door]
}*/
});
}
};
test.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
(function(){ // closure
load('javascript/main.js', 'javascript/jsunity.js');
 
jsUnity.log = print;
 
/**
* Attaches the following assertion methods to
* the global scope for convenience.
* - assertException
* - assertTrue
* - assertFalse
* - assertIdentical
* - assertNotIdentical
* - assertEqual
* - assertNotEqual
* - assertMatch
* - assertNotMatch
* - assertTypeOf
* - assertNotTypeOf
* - assertInstanceOf
* - assertNotInstanceOf
* - assertNull
* - assertNotNull
* - assertUndefined
* - assertNotUndefined
* - assertNaN
* - assertNotNaN
* - fail
*/
jsUnity.attachAssertions()
})();
 
jsUnity.run(function SomeTest ( )
{
function setUp ( )
{
print('setting up');
h = new Hallway();
}
 
function tearDown ( )
{
print('tearing down');
}
 
function test_pass_0 ( )
{
assertEqual([ 0, 0, 0, 0], h.state());
}
function test_pass_trip_one ( )
{
h.trip();
assertEqual([1, 1, 1, 1], h.state());
}
function test_pass_2 ( )
{
h.trips(2);
assertEqual([1, 0, 1, 0], h.state());
}
function test_pass_3 ( )
{
h.trips(3);
assertEqual([1,0,0,0], h.state());
}
function test_pass_4()
{
h.trips(4);
assertEqual([1,0,0,1], h.state());
}
function run_trips(amount, hallway){
for (amount in hallway){
hallway.trip();
}
return hallway;
}
}); // END jsUnity.run

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.