Skip to content

Conversation

konradrydzak
Copy link

@konradrydzak konradrydzak commented Apr 19, 2023

Fix sorting co2Scale to not be in place, change finding scaleIndex to be equal to closestNum (not greater than).

Previously co2Scale was sorted in place and scaleIndex was taken for a value greater than closestNum (if one existed, otherwise it would return -1).
After the changes, scaleIndex should correctly get the exact index of the closestNum value in co2Scale.

Probably also requires the same changes to Web-Dev-For-Beginners/5-browser-extension/solution/ and Web-Dev-For-Beginners/5-browser-extension/3-background-tasks-and-performance/translations/.

Fix sorting co2Scale to not be in place, change finding scaleIndex to be equal to closestNum (not greater than).
@github-actions
Copy link

This PR has not seen any action for a while! Closing for now, but it can be reopened at a later date.

@abrilurena
Copy link
Contributor

looks awesome! thanks @konradrydzak

I'm adding @saragibby here ♥️

@abrilurena abrilurena requested a review from saragibby November 15, 2023 01:07
@leestott leestott requested a review from Copilot October 3, 2025 08:52
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug in the calculateColor function where the co2Scale array was being sorted in-place and the scale index lookup was incorrectly finding elements greater than the closest value instead of equal to it.

  • Fixed array mutation by using slice() before sorting to avoid modifying the original co2Scale array
  • Changed the findIndex predicate from greater-than to equality comparison to find the exact index of the closest value

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +68 to 70
let closestNum = co2Scale.slice().sort((a, b) => {
return Math.abs(a - value) - Math.abs(b - value);
})[0];
Copy link
Preview

Copilot AI Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sorting logic finds the value with minimum absolute difference from the input value, but this doesn't guarantee it will be found in the original co2Scale array. If the input value is between two scale values, the closest might not exist in the original array, causing findIndex to return -1.

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants