Certainty of two comparative values (A/B results certainty) I'm trying to output the percentage of certainty of two comparatives data.

Here's the resulting code:

/**
 * Original code from https://vwo.com/js/significanceCalculator.js
 */
function calculatePValue (c_t, c_c, v_t, v_c) {
    var d1 = 0.0498673470,
        d2 = 0.0211410061,
        d3 = 0.0032776263,
        d4 = 0.0000380036,
        d5 = 0.0000488906,
        d6 = 0.0000053830;

    var c_p = c_c / c_t;
    var v_p = v_c / v_t;

    var std_error = Math.sqrt((c_p * (1 - c_p) / c_t) + (v_p * (1 - v_p) / v_t));
    var z_value = (v_p - c_p) / std_error;

    var a = Math.abs(z_value);
    var p_value = 1.0 + a * (d1 + a * (d2 + a * (d3 + a * (d4 + a * (d5 + a * d6)))));

    p_value *= p_value;
    p_value *= p_value;
    p_value *= p_value;
    p_value *= p_value;
    p_value = 1.0 / (p_value + p_value);
    if (z_value &gt;= 0)
        p_value = 1 - p_value;

    if (p_value &gt; 0.5)
        p_value = 1 - p_value;

    p_value = Math.round(p_value * 1000) / 1000;
    return p_value;
}

function getPercentage(totalA, convA, totalB, convB) {
    var p = calculatePValue(totalA, convA, totalB, convB);
    if (p &lt; 0,5) {
        return 'B, with certainty of : ' + ((1-p)*100).toFixed(2) + '%';
    } else {
        return 'A, with certainty of : ' + (p*100).toFixed(2) + '%';
    }
}


For example, if you try with:

getPercentage(1000, 120, 500, 90);


you'll get:


  "B, with certainty of : 99.90%"


The code for calculating P was extracted from here. I simply took it as a percentage value to display either A or B is better.

What do you think? Is this ok?
The code below doesn't reproduce your code entirely, it just illustrates what I mentioned earlier in the comments:

function example() {
    var values = [0.0498673470,
                  0.0211410061,
                  0.0032776263,
                  0.0000380036,
                  0.0000488906,
                  0.0000053830];
    function sum(a, b) { return a + b; }
    function sqdiff(a, b) { return (b - a) * (b - a); }
    function avg(data) {
        return data.reduce(sum) / data.length;
    }
    function curry(f) {
        var args = [].slice.call(arguments);
        args.shift();
        return function () {
            return f.apply(null, args.concat([].slice.call(arguments)));
        };
    }
    function std(data) {
        var a = avg(data);
        return Math.sqrt(data.map(curry(sqdiff, a)).reduce(sum) / 
            data.length);
    }
    console.log("Standard deviation: " + std(values));
}


You, most likely, won't want to write functions like curry yourself though. There are libraries out there which already do such (and many more) useful things. I believe that Lodash already has one.