Skip to content

Instantly share code, notes, and snippets.

@OsitaDNU
Last active October 25, 2023 07:35
Show Gist options
  • Save OsitaDNU/80a4eb12ce2b48f9023633412621c18f to your computer and use it in GitHub Desktop.
Save OsitaDNU/80a4eb12ce2b48f9023633412621c18f to your computer and use it in GitHub Desktop.
h-index Calculator (TypeScript)
// Example
// Calculates a h-index number from an array of random citations per research publication
// The number of research publications (e.g. journal articles)
let publications = 10;
// Maximum number of citations per publication
let maxCitation = 100;
// Generates an array of random citations per research publication
const citationsList: number[] = Array.from({length: publications}, () => Math.floor(Math.random() * maxCitation));
// Calculate h-index
console.log(hIndex(citationsList));
// The function
function hIndex(citations: number[]):number {
if (citations.length == 0) return 0;
const sortedCitations = citations.sort().reverse();
let tempIndex: number = 0;
for (let i = 0; i < sortedCitations.length; i++) {
if (sortedCitations[i] >= tempIndex) tempIndex++
else break;
}
return tempIndex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment