Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing Strict Array Bounds to Enhance User Safety and Prevent Out-of-Range Access #77

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

MarcusXavierr
Copy link

@MarcusXavierr MarcusXavierr commented Apr 16, 2024

JavaScript allows users to access values outside the range of an array, and as a consequence, some implementations of sorting and binary search algorithms that would fail in other languages work correctly in JavaScript, which in my opinion is incorrect.

I have created this PR to address this issue by adding a Proxy to throw an error when the user attempts to access a key that does not exist in the array.

Let's se a example:

This code seems correct, right?

export default function bubble_sort(arr: number[]): void {
  for (let i = 0; i <= arr.length; i++) {
    for (let j = 0; j <= arr.length - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        const temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
      }
    }
  }
}

And it passes the test
image

But there is an error on this code that can be caught using the strict array. On the inner loop, it access an index out of bound.

image

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.

1 participant