The game of forfeits is a simple game and one of the classic programming examples since 50 years. I found it in a ALGOL 60 exercise book from 1963: Count the numbers up to 100, but skip all numbers if one of the condition matches:

number is divisible by seven

number contains a seven

sum of digits is divisible by seven

sum of digits contains a seven

Develop a function which calculates all numbers based on the given conditions. The only one parameter for the function is the range and the function should return the Array of all valid numbers. Hint: You must not cheat while returning a sliced constant result array. The result of the function with max=100 is: [1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 18, 19, 20, 22, 23, 24, 26, 29, 30, 31, 32, 33, 36, 38, 39, 40, 41, 44, 45, 46, 48, 50, 51, 53, 54, 55, 58, 60, 62, 64, 65, 66, 69, 80, 81, 82, 83, 85, 88, 90, 92, 93, 94, 96, 99, 100].

clean code variant

Matthia’s has delivered a clean code variant for the problem and it will be used as the reference for this coding contest. It’s not the fastest solution but offers a direct implementation of the problem. Matthias uses an extra function for every condition and put it together in the main loop.

function cleanCodeSolution(n){ for (var i=0, res=[]; i <= n; i++) { if (!(containsSeven(i) || isMultipleOfSeven(i) || sumContainsSeven(i) || sumIsMultipleOfSeven(i))) { res.push(i); } } return res; //all helper functions defined in local scope function containsSeven (n) { return String(n).indexOf("7") !== -1; }; function isMultipleOfSeven (n) { return n % 7 === 0; }; function sumContainsSeven (n) { return containsSeven(sumOfDigits(n)); }; function sumIsMultipleOfSeven (n) { return isMultipleOfSeven(sumOfDigits(n)); } ; function sumOfDigits (n) { var sum = 0; while (n > 0) { sum += n % 10; n = Math.floor(n / 10); } return sum; }; }

If you are surprised about using the helper function before defining, read more about function defining the the In-depth guide to javascript functions.

Testing your solution

In the following textbox you can test your solution by pasting your javascript function body direct into it. It will be compared to Matthias’ clean code version and a performance optimized short clean code version, which works without functions.









/* game of forfeits var number {n} numbers to check return list of number */ function mySolution(n) {

// enter your code here

};

Result of your function

You can include my javascript test suite and add your solution. The benchmark will test your function only with

n=100

.

<script src="http://unitedcoderscom.appspot.com/sites/default/files/JSLitmus.js.txt" type="text/javascript"></script> <script src="http://unitedcoderscom.appspot.com/sites/default/files/forfeits.js.txt" type="text/javascript"></script> <script type="text/javascript"> JSLitmus.test("clean code version", com.unitedCoders.forfeits.testClean); JSLitmus.test("short version", com.unitedCoders.forfeits.testShort); if (com.unitedCoders.forfeits.runTest(myFunction) { JSLitmus.test("your version", function() { myFunction(100); } ); }

more sultions

In older articles (javascript lotto generator or articles with code puzzles tag), here on unitedcoderscom.appspot.com we linked articles and got comments with solutions in other languages. Feel free to comment or use our tackbacks, but only javascript have the possibility to run directly in the browser.

I will post in the second article the original ALGOL 60 solutions and my 1:1 migration to the javascript variant to benchmark it with today's coding skills. If we got some interesting solutions the third article will comment, link and benchmark all.