How to resolve the algorithm Ranking methods step by step in the JavaScript programming language
How to resolve the algorithm Ranking methods step by step in the JavaScript programming language
Table of Contents
Problem Statement
The numerical rank of competitors in a competition shows if one is better than, equal to, or worse than another based on their results in a competition. The numerical rank of a competitor can be assigned in several different ways.
The following scores are accrued for all competitors of a competition (in best-first order): For each of the following ranking methods, create a function/method/procedure/subroutine... that applies the ranking method to an ordered list of scores with scorers:
See the wikipedia article for a fuller description. Show here, on this page, the ranking of the test scores under each of the numbered ranking methods.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Ranking methods step by step in the JavaScript programming language
Scope and module pattern
The source code is an implementation of the module pattern in JavaScript. The module pattern is a design pattern that allows us to encapsulate code within a closure, creating a private scope for the variables and functions defined within the module.
In this case, the module pattern is implemented using an anonymous immediately-invoked function expression (IIFE), which is a function that is immediately invoked upon definition.
The IIFE creates a new scope for the variables and functions defined within it, which are not accessible from outside the module.
The module pattern is commonly used to organize code into reusable and self-contained units, and to prevent global scope pollution.
Data manipulation
The source code performs several data manipulation operations on an array of names and an array of scores. The first operation is to sort the names and scores by score, in descending order. The next operation is to create an array of unique scores, which are the scores that appear only once in the original array. The final operation is to calculate several rankings for each score, including the standard ranking, the modified ranking, the dense ranking, and the fractional ranking.
Table generation
The last part of the source code uses the data manipulation results to generate a table in the Wiki markup language.
The table is generated using the wikiTable
function, which takes an array of rows, a boolean indicating whether the first row is a header row, and an optional string containing CSS styles to apply to the table.
The wikiTable
function generates a table with the specified rows and styles, and returns the table as a string.
The table is then printed to the console using the console.log()
function.
Conclusion
The source code demonstrates several useful techniques for data manipulation and table generation in JavaScript, and it is a good example of how to use the module pattern to encapsulate code within a private scope.
Source code in the javascript programming language
(function () {
var xs = 'Solomon Jason Errol Garry Bernard Barry Stephen'.split(' '),
ns = [44, 42, 42, 41, 41, 41, 39],
sorted = xs.map(function (x, i) {
return { name: x, score: ns[i] };
}).sort(function (a, b) {
var c = b.score - a.score;
return c ? c : a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
}),
names = sorted.map(function (x) { return x.name; }),
scores = sorted.map(function (x) { return x.score; }),
reversed = scores.slice(0).reverse(),
unique = scores.filter(function (x, i) {
return scores.indexOf(x) === i;
});
// RANKINGS AS FUNCTIONS OF SCORES: SORTED, REVERSED AND UNIQUE
var rankings = function (score, index) {
return {
name: names[index],
score: score,
Ordinal: index + 1,
Standard: function (n) {
return scores.indexOf(n) + 1;
}(score),
Modified: function (n) {
return reversed.length - reversed.indexOf(n);
}(score),
Dense: function (n) {
return unique.indexOf(n) + 1;
}(score),
Fractional: function (n) {
return (
(scores.indexOf(n) + 1) +
(reversed.length - reversed.indexOf(n))
) / 2;
}(score)
};
},
tbl = [
'Name Score Standard Modified Dense Ordinal Fractional'.split(' ')
].concat(scores.map(rankings).reduce(function (a, x) {
return a.concat([
[x.name, x.score,
x.Standard, x.Modified, x.Dense, x.Ordinal, x.Fractional
]
]);
}, [])),
//[[a]] -> bool -> s -> s
wikiTable = function (lstRows, blnHeaderRow, strStyle) {
return '{| class="wikitable" ' + (
strStyle ? 'style="' + strStyle + '"' : ''
) + lstRows.map(function (lstRow, iRow) {
var strDelim = ((blnHeaderRow && !iRow) ? '!' : '|');
return '\n|-\n' + strDelim + ' ' + lstRow.map(function (v) {
return typeof v === 'undefined' ? ' ' : v;
}).join(' ' + strDelim + strDelim + ' ');
}).join('') + '\n|}';
};
return wikiTable(tbl, true, 'text-align:center');
})();
((() => {
const xs = 'Solomon Jason Errol Garry Bernard Barry Stephen'.split(' '),
ns = [44, 42, 42, 41, 41, 41, 39];
const sorted = xs.map((x, i) => ({
name: x,
score: ns[i]
}))
.sort((a, b) => {
const c = b.score - a.score;
return c ? c : a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
});
const names = sorted.map(x => x.name),
scores = sorted.map(x => x.score),
reversed = scores.slice(0)
.reverse(),
unique = scores.filter((x, i) => scores.indexOf(x) === i);
// RANKINGS AS FUNCTIONS OF SCORES: SORTED, REVERSED AND UNIQUE
// rankings :: Int -> Int -> Dictonary
const rankings = (score, index) => ({
name: names[index],
score,
Ordinal: index + 1,
Standard: scores.indexOf(score) + 1,
Modified: reversed.length - reversed.indexOf(score),
Dense: unique.indexOf(score) + 1,
Fractional: (n => (
(scores.indexOf(n) + 1) +
(reversed.length - reversed.indexOf(n))
) / 2)(score)
});
// tbl :: [[[a]]]
const tbl = [
'Name Score Standard Modified Dense Ordinal Fractional'.split(' ')
].concat(scores.map(rankings)
.reduce((a, x) => a.concat([
[x.name, x.score,
x.Standard, x.Modified, x.Dense, x.Ordinal, x.Fractional
]
]), []));
// wikiTable :: [[[a]]] -> Bool -> String -> String
const wikiTable = (lstRows, blnHeaderRow, strStyle) =>
`{| class="wikitable" ${strStyle ? 'style="' + strStyle + '"' : ''}
${lstRows.map((lstRow, iRow) => {
const strDelim = ((blnHeaderRow && !iRow) ? '!' : '|');
return '\n|-\n' + strDelim + ' ' + lstRow
.map(v => typeof v === 'undefined' ? ' ' : v)
.join(' ' + strDelim + strDelim + ' ');
}).join('')}\n|}`;
return wikiTable(tbl, true, 'text-align:center');
}))();
You may also check:How to resolve the algorithm Host introspection step by step in the Haskell programming language
You may also check:How to resolve the algorithm Literals/String step by step in the Metafont programming language
You may also check:How to resolve the algorithm Matrix transposition step by step in the Rust programming language
You may also check:How to resolve the algorithm Read a file line by line step by step in the C++ programming language
You may also check:How to resolve the algorithm Bitcoin/public point to address step by step in the Haskell programming language