I'm new to Node.js and finally making some headway before running into 
module.exports

I wanted to separate some code out into an external file, so I created the 
file and called it probability.js

The problem I ran into was having to define the function getRandomInt twice 
to allow it to be called by the Die methods and by the primary server code. 
 I'm sure this is a scope issue, but I have not been able to figure out how 
to resolve it in the proper way.  Ideally, I want the getRandomInt function 
defined one time within module.exports, but not part of Die or Dice objects 
while still allowing the Die and Dice methods to call it.  Any assistance 
is appreciated.



module.exports = {    
    
    // Returns a random integer between min and max
    // Using Math.round() will give you a non-uniform distribution!
    getRandomInt: function(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    },
    
    Die: function(numSides) {
        // constructor;
        var sides = numSides;
        var lastRoll = -1;
        
        this.roll = function() {
            console.log("sides: %d", sides);
            lastRoll = getRandomInt(1, sides);
        }
    
        this.getLastRoll = function() {
            return lastRoll;
        }
    },
    
    Dice: function() {
        var myDie = new Array();
        var totalDie = 0;
        
        this.addDie = function(newDie) {
            myDie.push(newDie);
            totalDie = totalDie + 1;
        }
        
        this.roll = function() {
            for (i=0; i<totalDie; i++) {
                myDie[i].roll();
            }
        }
        
        this.getTotalRoll = function() {
            var total = 0;
            
            console.log("Get Total Roll");
            for (i=0; i<totalDie; i++) {
                console.log('Dice: %d = %d', i, myDie[i].getLastRoll());
                total = total + myDie[i].getLastRoll();
            }
            
            return total;
        }
    }
};

// Returns a random integer between min and max
// Using Math.round() will give you a non-uniform distribution!
var getRandomInt = function(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    }


// Returns a random number between 0 (inclusive) and 1 (exclusive)
var getRandom = function () {
    return Math.random();
}

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to