Editing Exotic add function in JavaScript This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Comparison between add functions. [View the gist here](https://gist.github.com/4575816) Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) function add_normal() { // Start from zero var total = 0; // Floating-points, bah! // 1e12 = 1000000000000. var factor = 1e12; // Undefined, set in the loop var value; // Something to iterate on var i = arguments.length; // Loop through all the parameters while (i--) { // Multiply by 1e12, to account for peculiarities // of doing addition with floating-point numbers. value = parseFloat(arguments[i]) * factor; // Is it not, not a number? // Then hey, it's a number! if (!isNaN(value)) { total += value; } } // Divide back by 1e12, because we multiplied by // 1e12 to account for floating-point weirdness. return total/factor; } // can you figure it? function add_exotic() { var args = [].slice.call(arguments), total = 0; for (;args.length; args.shift()) { !args[0] || typeof args[0] === 'boolean' || (total += parseFloat( !(args[0]*1) ? 0 : args[0] ) * 1e12); } return total/1e12; } Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code // Should equal 15 add_normal(1, 2, 3, 4, 5); // Should equal 0 add_normal(5, null, -5); // Should equal 10 add_normal('1.0', false, 1, true, 1, 'A', 1, 'B', 1, 'C', 1, 'D', 1, 'E', 1, 'F', 1, 'G', 1); // Should equal 0.3, not 0.30000000000000004 add_normal(0.1, 0.2); Test 2 Title Async (check if this is an asynchronous test) Code // Should equal 15 add_exotic(1, 2, 3, 4, 5); // Should equal 0 add_exotic(5, null, -5); // Should equal 10 add_exotic('1.0', false, 1, true, 1, 'A', 1, 'B', 1, 'C', 1, 'D', 1, 'E', 1, 'F', 1, 'G', 1); // Should equal 0.3, not 0.30000000000000004 add_exotic(0.1, 0.2);