kata-machine
kata-machine copied to clipboard
Implementing Strict Array Bounds to Enhance User Safety and Prevent Out-of-Range Access
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
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.