Javascript: H-Index… I’m working on the H-index leetcode problem, why is my function exceeding time limit? [closed]

/**

  • @param {number[]} citations
  • @return {number}
    */

var hIndex = function(citations) {

let mid = Math.floor(citations.length / 2);

citations.sort((a, b) => b - a)

while (citations[mid] >= mid) {
    if (citations[mid] < mid + 1) {
        mid--
    } else if (citations[mid] > mid + 1) {
        mid++
    } 
}
return mid;

};

I think what’s causing the issue is the while loop, but I just couldn’t figure out why…. Even if my answer might be incorrect, I still want to figure out the reason what happens, so I can avoid the same mistake in the future! Thanks!