Skip to content

Instantly share code, notes, and snippets.

@vnegi10
Created May 10, 2024 10:20
Show Gist options
  • Save vnegi10/18b54b41dd0e2fc4524370d447ee622c to your computer and use it in GitHub Desktop.
Save vnegi10/18b54b41dd0e2fc4524370d447ee622c to your computer and use it in GitHub Desktop.
export function getTopPerformer(breakdown, num_days) {
    const changeArray = []
    for (let i = 0; i < names.length; i++) {
        const changeObj = {
            name: names[i],
            change: (breakdown[breakdown.length - 1][names[i]] -
                    breakdown[breakdown.length - 1 - num_days][names[i]])
                    / breakdown[breakdown.length - 1- num_days][names[i]]
        }
        changeArray.push(changeObj)
    }
    // Find maximum value and its corresponding index in changeArray
    let maxValue = -Infinity;
    let maxIndex = -1;
    for (let i = 0; i < changeArray.length; i++) {
      if (changeArray[i].change > maxValue) {
        maxValue = changeArray[i].change;
        maxIndex = i;
      }
    }
    // Change %
    const maxChange = maxValue * 100;
    // Round off to first digit after decimal
    const roundMaxChange = Math.round(maxChange * 10) / 10;
    const resultObj = {
        maxChange: roundMaxChange,
        maxName: tickers[maxIndex]
    }
    return resultObj
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment