├── Template ├── template.js ├── templateTest.js └── Template.conf ├── BinaryChop ├── BinaryChop.conf ├── binaryChop.js └── binaryChopTest.js ├── lib ├── JsTestDriver-1.2.2.jar └── launch-safari.sh ├── BowlingGame ├── BowlingGame.conf ├── BowlingGameTest.js └── BowlingGame.js ├── PrimeFactors ├── PrimeFactors.conf ├── primeFactors.js └── primeFactorsTest.js ├── JsTestDriver.bat └── JsTestDriver.sh /Template/template.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Template/templateTest.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Template/Template.conf: -------------------------------------------------------------------------------- 1 | server: http://localhost:8081 2 | 3 | load: 4 | - template.js 5 | - templateTest.js -------------------------------------------------------------------------------- /BinaryChop/BinaryChop.conf: -------------------------------------------------------------------------------- 1 | server: http://localhost:8081 2 | 3 | load: 4 | - binaryChop.js 5 | - binaryChopTest.js -------------------------------------------------------------------------------- /lib/JsTestDriver-1.2.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zachleat/JavaScript-Code-Katas/HEAD/lib/JsTestDriver-1.2.2.jar -------------------------------------------------------------------------------- /BowlingGame/BowlingGame.conf: -------------------------------------------------------------------------------- 1 | server: http://localhost:8081 2 | 3 | load: 4 | - BowlingGame.js 5 | - BowlingGameTest.js -------------------------------------------------------------------------------- /PrimeFactors/PrimeFactors.conf: -------------------------------------------------------------------------------- 1 | server: http://localhost:8081 2 | 3 | load: 4 | - primeFactors.js 5 | - primeFactorsTest.js -------------------------------------------------------------------------------- /lib/launch-safari.sh: -------------------------------------------------------------------------------- 1 | #SAFARI="/Applications/Safari.app/Contents/MacOS/Safari" 2 | #exec "$SAFARI" $1 3 | 4 | open -W -a Safari $1 -------------------------------------------------------------------------------- /JsTestDriver.bat: -------------------------------------------------------------------------------- 1 | rem Usage: JsTestDriver.bat TestName 2 | rem where TestName is a directory and points to TestName/TestName.conf. 3 | 4 | java -jar lib/JsTestDriver-1.2.2.jar --config "%1/%1.conf" --browser "C:\Program Files\Internet Explorer\iexplore.exe" --tests all --port 8081 -------------------------------------------------------------------------------- /JsTestDriver.sh: -------------------------------------------------------------------------------- 1 | # Usage: ./JsTestDriver.sh TestName 2 | # where TestName is a directory and TestName.conf in the root. 3 | 4 | java -jar lib/JsTestDriver-1.2.2.jar --config "$1/$1.conf" --browser "lib/launch-safari.sh" --tests all --port 8081 5 | osascript -e 'tell application "Safari" to quit' > /dev/null 2>&1 6 | -------------------------------------------------------------------------------- /PrimeFactors/primeFactors.js: -------------------------------------------------------------------------------- 1 | PrimeFactors = {}; 2 | PrimeFactors.generate = function(n) 3 | { 4 | var primes = []; 5 | for(var candidate = 2; n > 1; candidate++) { 6 | for(; n % candidate === 0; n /= candidate) { 7 | primes.push(candidate); 8 | } 9 | } 10 | return primes; 11 | }; -------------------------------------------------------------------------------- /BinaryChop/binaryChop.js: -------------------------------------------------------------------------------- 1 | function BinaryChop(needle, haystack, minIndex, maxIndex) 2 | { 3 | if(!haystack.length) { 4 | return -1; 5 | } 6 | 7 | if(typeof minIndex === 'undefined' && typeof maxIndex === 'undefined') { 8 | minIndex = 0; 9 | maxIndex = haystack.length - 1; 10 | } 11 | 12 | var middle = minIndex + Math.floor((maxIndex - minIndex)/2); 13 | if(needle === haystack[middle]) { 14 | return middle; 15 | } else if(maxIndex === minIndex) { 16 | return -1; 17 | } else if(needle < haystack[middle]) { 18 | return arguments.callee(needle, haystack, minIndex, middle); 19 | } else if(needle > haystack[middle]) { 20 | return arguments.callee(needle, haystack, middle + 1, maxIndex); 21 | } 22 | }; -------------------------------------------------------------------------------- /PrimeFactors/primeFactorsTest.js: -------------------------------------------------------------------------------- 1 | PrimeFactorsTest = TestCase("PrimeFactorsTest"); 2 | 3 | PrimeFactorsTest.prototype.testOne = function(){ 4 | assertEquals([], PrimeFactors.generate(1)); 5 | }; 6 | 7 | PrimeFactorsTest.prototype.testTwo = function(){ 8 | assertEquals([2], PrimeFactors.generate(2)); 9 | }; 10 | 11 | PrimeFactorsTest.prototype.testThree = function() 12 | { 13 | assertEquals([3], PrimeFactors.generate(3)); 14 | }; 15 | 16 | PrimeFactorsTest.prototype.testFour = function() 17 | { 18 | assertEquals([2,2], PrimeFactors.generate(4)); 19 | }; 20 | 21 | PrimeFactorsTest.prototype.testSix = function() 22 | { 23 | assertEquals([2,3], PrimeFactors.generate(6)); 24 | }; 25 | 26 | PrimeFactorsTest.prototype.testEight = function() 27 | { 28 | assertEquals([2,2,2], PrimeFactors.generate(8)); 29 | }; 30 | 31 | PrimeFactorsTest.prototype.testNine = function() 32 | { 33 | assertEquals([3,3], PrimeFactors.generate(9)); 34 | }; -------------------------------------------------------------------------------- /BowlingGame/BowlingGameTest.js: -------------------------------------------------------------------------------- 1 | BowlingGameTest = TestCase('BowlingGameTest'); 2 | 3 | BowlingGameTest.prototype.setUp = function() 4 | { 5 | this.g = new Game(); 6 | }; 7 | 8 | BowlingGameTest.prototype.rollMany = function(n, pins) 9 | { 10 | for(var j = 0; j < n; j++) { 11 | this.g.roll(pins); 12 | } 13 | }; 14 | 15 | BowlingGameTest.prototype.rollSpare = function() 16 | { 17 | this.g.roll(5); 18 | this.g.roll(5); 19 | }; 20 | 21 | BowlingGameTest.prototype.rollStrike = function() 22 | { 23 | this.g.roll(10); 24 | }; 25 | 26 | BowlingGameTest.prototype.testGutterGame = function() 27 | { 28 | this.rollMany(20, 0); 29 | assertEquals(0, this.g.score()); 30 | }; 31 | 32 | BowlingGameTest.prototype.testAllOnes = function() 33 | { 34 | this.rollMany(20, 1); 35 | assertEquals(20, this.g.score()); 36 | }; 37 | 38 | BowlingGameTest.prototype.testOneSpare = function() 39 | { 40 | this.rollSpare(); 41 | this.g.roll(3); 42 | this.rollMany(17, 0); 43 | 44 | assertEquals(16, this.g.score()); 45 | }; 46 | 47 | BowlingGameTest.prototype.testOneStrike = function() 48 | { 49 | this.rollStrike(); 50 | this.g.roll(3); 51 | this.g.roll(4); 52 | this.rollMany(16, 0); 53 | 54 | assertEquals(24, this.g.score()); 55 | }; 56 | 57 | BowlingGameTest.prototype.testPerfectGame = function() 58 | { 59 | this.rollMany(12, 10); 60 | assertEquals(300, this.g.score()); 61 | }; 62 | -------------------------------------------------------------------------------- /BowlingGame/BowlingGame.js: -------------------------------------------------------------------------------- 1 | function Game() 2 | { 3 | this.rolls = []; 4 | this.currentRoll = 0; 5 | } 6 | 7 | Game.prototype.roll = function(pins) 8 | { 9 | this.rolls[this.currentRoll++] = pins; 10 | }; 11 | 12 | Game.prototype.score = function() 13 | { 14 | var points = 0, 15 | frameIndex = 0; 16 | 17 | for(var frame = 0; frame<10; frame++) { 18 | if(this.isStrike(frameIndex)) { 19 | points += this.strikeBonus(frameIndex); 20 | frameIndex++; 21 | } else if(this.isSpare(frameIndex)) { 22 | points += this.spareBonus(frameIndex); 23 | frameIndex += 2; 24 | } else { 25 | points += this.sumOfBallsInFrame(frameIndex); 26 | frameIndex += 2; 27 | } 28 | } 29 | 30 | return points; 31 | }; 32 | 33 | Game.prototype.isSpare = function(frameIndex) 34 | { 35 | return this.rolls[frameIndex] + this.rolls[frameIndex+1] == 10; 36 | }; 37 | 38 | Game.prototype.spareBonus = function(frameIndex) 39 | { 40 | return 10 + this.rolls[frameIndex+2]; 41 | }; 42 | 43 | Game.prototype.isStrike = function(frameIndex) 44 | { 45 | return this.rolls[frameIndex] == 10; 46 | }; 47 | 48 | Game.prototype.strikeBonus = function(frameIndex) 49 | { 50 | return 10 + this.rolls[frameIndex+1] + this.rolls[frameIndex+2]; 51 | }; 52 | 53 | Game.prototype.sumOfBallsInFrame = function(frameIndex) 54 | { 55 | return this.rolls[frameIndex] + (this.rolls[frameIndex+1] || 0); 56 | }; 57 | -------------------------------------------------------------------------------- /BinaryChop/binaryChopTest.js: -------------------------------------------------------------------------------- 1 | BinaryChopTest = TestCase('BinaryChop'); 2 | 3 | BinaryChopTest.prototype.testLengthZero = function() 4 | { 5 | assertEquals(-1, BinaryChop(0, [])); 6 | }; 7 | 8 | BinaryChopTest.prototype.testLengthOne = function() 9 | { 10 | assertEquals(0, BinaryChop(1, [1])); 11 | assertEquals(-1, BinaryChop(0, [1])); 12 | assertEquals(-1, BinaryChop(2, [1])); 13 | }; 14 | 15 | BinaryChopTest.prototype.testLengthTwo = function() 16 | { 17 | assertEquals(0, BinaryChop(1, [1,3])); 18 | assertEquals(1, BinaryChop(3, [1,3])); 19 | assertEquals(-1, BinaryChop(0, [1,3])); 20 | assertEquals(-1, BinaryChop(2, [1,3])); 21 | assertEquals(-1, BinaryChop(4, [1,3])); 22 | }; 23 | 24 | BinaryChopTest.prototype.testLengthThree = function() 25 | { 26 | assertEquals(0, BinaryChop(1, [1,3,5])); 27 | assertEquals(1, BinaryChop(3, [1,3,5])); 28 | assertEquals(2, BinaryChop(5, [1,3,5])); 29 | assertEquals(-1, BinaryChop(0, [1,3,5])); 30 | assertEquals(-1, BinaryChop(2, [1,3,5])); 31 | assertEquals(-1, BinaryChop(4, [1,3,5])); 32 | assertEquals(-1, BinaryChop(6, [1,3,5])); 33 | }; 34 | 35 | BinaryChopTest.prototype.testLengthFour = function() 36 | { 37 | assertEquals(0, BinaryChop(1, [1,3,5,7])); 38 | assertEquals(1, BinaryChop(3, [1,3,5,7])); 39 | assertEquals(2, BinaryChop(5, [1,3,5,7])); 40 | assertEquals(3, BinaryChop(7, [1,3,5,7])); 41 | assertEquals(-1, BinaryChop(0, [1,3,5,7])); 42 | assertEquals(-1, BinaryChop(2, [1,3,5,7])); 43 | assertEquals(-1, BinaryChop(4, [1,3,5,7])); 44 | assertEquals(-1, BinaryChop(6, [1,3,5,7])); 45 | assertEquals(-1, BinaryChop(8, [1,3,5,7])); 46 | }; --------------------------------------------------------------------------------