Last active
October 25, 2023 07:35
-
-
Save OsitaDNU/80a4eb12ce2b48f9023633412621c18f to your computer and use it in GitHub Desktop.
h-index Calculator (TypeScript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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