25-Jun-14 (Created: 25-Jun-14) | More in 'CS-JavaScript'

Sometimes Abstracted Object Oriented code is really pretty!

Consider this


function Pair(inMin, inMax)
{
    this.min = inMin;
    this.max = inMax;
}

Which would let me use


var somepair = new Pair(5,10);
somepair.min => 5
somepair.max => 10

See this function now


function getTotalScaledScorePair()
{
    var rpair = getReadingScaledScorePair();
    var wpair = getWritingScaledScorePair();
    var mpair = getMathScaledScorePair();
    
    var tmin = rpair.min + wpair.min + mpair.min;
    var tmax = rpair.max + wpair.max + mpair.max;
    return new Pair(tmin, tmax);
}

What I like? Very clear, mostly typed, less likely to make mistakes. Here are some other ways this Pair is used


//SAT.getReadingScaledScorePair = getReadingScaledScorePair;
function getReadingScaledScorePair()
{
    var rawscore = getReadingRawScore();
    var rawscore = Math.ceil(rawscore);
    var vScaledScore =ScaledScoreMap[rawscore];
    return new Pair(vScaledScore.readingMin, vScaledScore.readingMax);
}
//SAT.getWritingScaledScorePair = getWritingScaledScorePair;
function getWritingScaledScorePair()
{
    var rawscore = getWritingRawScore();
    var rawscore = Math.ceil(rawscore);
    var vScaledScore =ScaledScoreMap[rawscore];
    return new Pair(vScaledScore.writingMin, vScaledScore.writingMax);
}
//SAT.getMathScaledScorePair = getMathScaledScorePair;
function getMathScaledScorePair()
{
    var rawscore = getMathRawScore();
    var rawscore = Math.ceil(rawscore);
    var vScaledScore =ScaledScoreMap[rawscore];
    return new Pair(vScaledScore.mathMin, vScaledScore.mathMax);
}

here is how all this is brouht together in html


<div class="aspire_app_sat_form">
<p class="medium">Reading: <strong class="attention medium">
<script>
var rpair = SAT.getReadingScaledScorePair();
document.write(rpair.min);
document.write(" - ");
document.write(rpair.max);
</script>
</strong></p>

<p class="medium">Writing: <strong class="attention medium">
<script>
var rpair = SAT.getWritingScaledScorePair();
document.write(rpair.min);
document.write(" - ");
document.write(rpair.max);
</script>
</strong></p>

<p class="medium">Math: <strong class="attention medium">
<script>
var rpair = SAT.getMathScaledScorePair();
document.write(rpair.min);
document.write(" - ");
document.write(rpair.max);
</script>
</strong></p>

<p class="medium">Total: <strong class="attention medium">
<script>
var rpair = SAT.getTotalScaledScorePair();
document.write(rpair.min);
document.write(" - ");
document.write(rpair.max);
</script>
</strong></p>
</div>

Here is the whole .js file (very preliminary and may contain mistakes and half finished code)


//****************************************************************
//Introduce a global variable called SAT
//SAT holds the common functions and objects for javascript
//SAT does the scoping for javascript functions
//
//Also see akc-widget.js
//****************************************************************

var SAT;
if (!SAT) {
    SAT = {};
}

//****************************************************************
//Attach a series of functions and objects to SAT
//This allows you call or instantiate with things like 
//  new SAT.someObject
//  or call SAT.someFunction
//****************************************************************
(function () {

//***********************************************
// Public Interface
//
//Note this is not a function but a class
//Public variables, functions, classes
//************************************************
SAT.registerSectionScore = registerSectionScore;
SAT.getReadingRawScore = getReadingRawScore;
SAT.getWritingRawScore = getWritingRawScore;
SAT.getMathRawScore = getMathRawScore;
SAT.getReadingScaledScorePair = getReadingScaledScorePair;
SAT.getWritingScaledScorePair = getWritingScaledScorePair;
SAT.getMathScaledScorePair = getMathScaledScorePair;
SAT.getTotalScaledScorePair = getTotalScaledScorePair;

function getTotalScaledScorePair()
{
    var rpair = getReadingScaledScorePair();
    var wpair = getWritingScaledScorePair();
    var mpair = getMathScaledScorePair();
    
    var tmin = rpair.min + wpair.min + mpair.min;
    var tmax = rpair.max + wpair.max + mpair.max;
    return new Pair(tmin, tmax);
}

function pageLoaded()
{
    //alert('page loaded');
}    

var ScaledScoreMap = {};
var readingRawScore = 0;
var writingRawScore = 0;
var essayRawScore = 16;
var mathRawScore = 0;

//SAT.registerSectionScore = registerSectionScore;

function registerSectionScore(sectionname, sectionscore)
{
    if (sectionname.indexOf("eading") >= 0)
    {
        //reading exists
        readingRawScore += sectionscore;
    }
    else if (sectionname.indexOf("riting") >= 0)
    {
        //reading exists
        writingRawScore += sectionscore;
    }
    else if (sectionname.indexOf("ath") >= 0)
    {
        //reading exists
        mathRawScore += sectionscore;
    }
    else
    {
        alert("Invalid sectionname:" + sectionname);
    }
}

//SAT.registerSectionScore = registerSectionScore;
//SAT.getReadingRawScore = getReadingRawScore;
//SAT.getWritingRawScore = getWritingRawScore;
//SAT.getMathRawScore = getMathRawScore;

function getReadingRawScore()
{
    return readingRawScore;
}
function getWritingRawScore()
{
    return writingRawScore + essayRawScore;
}
function getMathRawScore()
{
    return mathRawScore;
}

function ScaledScore(rMin, rMax, wMin, wMax, mMin, mMax)
{
    this.readingMin = rMin;
    this.readingMax = rMax;
    
    this.writingMin = wMin;
    this.writingMax = wMax;
    
    this.mathMin = mMin;
    this.mathMax = mMax;
}    

//SAT.getReadingScaledScorePair = getReadingScaledScorePair;
function getReadingScaledScorePair()
{
    var rawscore = getReadingRawScore();
    var rawscore = Math.ceil(rawscore);
    var vScaledScore =ScaledScoreMap[rawscore];
    return new Pair(vScaledScore.readingMin, vScaledScore.readingMax);
}
//SAT.getWritingScaledScorePair = getWritingScaledScorePair;
function getWritingScaledScorePair()
{
    var rawscore = getWritingRawScore();
    var rawscore = Math.ceil(rawscore);
    var vScaledScore =ScaledScoreMap[rawscore];
    return new Pair(vScaledScore.writingMin, vScaledScore.writingMax);
}
//SAT.getMathScaledScorePair = getMathScaledScorePair;
function getMathScaledScorePair()
{
    var rawscore = getMathRawScore();
    var rawscore = Math.ceil(rawscore);
    var vScaledScore =ScaledScoreMap[rawscore];
    return new Pair(vScaledScore.mathMin, vScaledScore.mathMax);
}

function Pair(inMin, inMax)
{
    this.min = inMin;
    this.max = inMax;
}
function initScaledScoreMap()
{
    //alert('initializing the map');
    
    ScaledScoreMap[1] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[2] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[3] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[4] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[5] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[6] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[7] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[8] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[9] = new ScaledScore(10,12,30,40,50,60);
    
    ScaledScoreMap[10] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[11] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[12] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[13] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[14] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[15] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[16] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[17] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[18] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[19] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[20] = new ScaledScore(10,12,30,40,50,60);
    
    ScaledScoreMap[21] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[22] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[23] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[24] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[26] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[27] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[28] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[29] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[30] = new ScaledScore(10,12,30,40,50,60);

    ScaledScoreMap[31] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[32] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[33] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[34] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[35] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[36] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[37] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[38] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[39] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[40] = new ScaledScore(10,12,30,40,50,60);
    
    ScaledScoreMap[41] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[42] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[43] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[44] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[45] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[46] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[47] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[48] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[49] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[50] = new ScaledScore(10,12,30,40,50,60);
    
    ScaledScoreMap[61] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[62] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[63] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[64] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[65] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[66] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[67] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[68] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[69] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[70] = new ScaledScore(10,12,30,40,50,60);

    ScaledScoreMap[71] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[72] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[73] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[74] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[75] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[76] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[77] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[78] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[79] = new ScaledScore(10,12,30,40,50,60);
    ScaledScoreMap[80] = new ScaledScore(10,12,30,40,50,60);
    //alert('Done initializing the map');
}
//Prime the pump
$(document).ready(pageLoaded);
initScaledScoreMap();
    
})(); //eof-file