HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Genetic Drift Simulator - Follow Up

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
driftgeneticsimulatorfollow

Problem

This question is a follow-up of a previous question of mine: Genetic Drift Simulator.

Following the advice given in the last question, and some more general improvements, the code has been slimmed down a bit. The major one was implementing Underscore.js, which slimmed down a few of the loops into functions.

The config has identical properties to the last question, however, they've been renamed.

  • numGenerations: the number of generations to iterate over. Can be number input or infinite to go until one population remains.



  • population: an object containing the names of the populations and the amount of each.



  • drawAmount: the amount of population to be surveyed.



var GeneticDriftSimulator = function (user_config) {
    this.config = _.defaults(user_config, {
        numGenerations: '5',
        population: {
            red: 50,
            blue: 50
        },
        drawAmount: 10
    });
};

GeneticDriftSimulator.prototype.run = function () {
    var ratioPopulation = this.config.population;
    var isIndefinite = (this.config.numGenerations == 'infinite');
    for (var i = 0; isIndefinite || i = population.length) {
        throw 'SimulationError: The DrawAmount is more than the Population.';
    }
    if (population.length % drawAmount != 0) {
        throw 'SimulationError: The DrawAmount cannot be evenly divided by Population';
    }

    temp_drawn = _.sample(population, drawAmount);
    var multiplier = population.length / drawAmount;
    return this.RatioCount(multiplier, temp_drawn);
};

GeneticDriftSimulator.prototype.RatioCount = function (multiplier, temp_drawn) {
    var final = {};
    _.each(temp_drawn, function (population) {
        for (var i = 0; i < multiplier; i++) {
            if (!(population in final)) {
                final[population] = 0;
            }
            final[population]++;
        }
    });
    return final;
}


with usage like the following:

```
var config = {
numGenerations: 'infinite',
populat

Solution

A few things to point out in retrospect:

  • There's inconsistent quotation marks everywhere



  • Lines like the following are confusing and hard to read: print_result += key + ':' + ratioPopulation[key] + ' '



  • These (throw) should be throw new Error as it includes a stack trace.



  • Don't use snake case (temp_drawn) use camel case (tempDrawn)



  • Be careful about using truthy value checks like (population.length % drawAmount != 0) as it can cause issues relating to booleans, for example false == 0 will return true, but false === 0 will return false. See Truthy and Falsy: When All is Not Equal in JavaScript for more information and examples.



  • run doesn't match the case of the other functions, it should be Run.



  • k



  • You should consider using an array here instead var final = {};` as it may reduce logic on the loops.

Context

StackExchange Code Review Q#107351, answer score: 6

Revisions (0)

No revisions yet.