0

Is it possible to create an out of bounds error with a for loop using an arrays length in JavaScript?

For example:

const strArr = ["", "", ""];

try {
  for (let i = 0; i <= strArr.length; i++) {}
} catch (err) {
  console.log("Array out of bounds error occurred.");
  console.log(err);
}

I know this sounds silly, but how can I make this for loop trigger an out of bounds error? This is just for educational purposes, as I cant find any examples of out of bounds for loop errors elsewhere online.

In this case, wrong answers are correct.

This is the original delivery of the University question/directions:

1.) create an array of strings.

2.) create a try/catch block.

3.) In the try portion of the try/catch, you will loop through the array using a ‘for loop’ where your index variable begins at 0 and the loop continues as long as the index is <= the length of the array. This will cause an array out of bounds error where the loop attempts to access an array index value that is one more than the last item.

4.) The catch portion will catch the out of bounds exception. Inside the catch block you will write two lines to the console. The first simply prints the string, “Array out of bounds error occurred.” On a new line you will then print the error message contained in the exception object.

twominds
  • 1,084
  • 5
  • 20
  • 2
    Accessing an array index beyond array.length doesn't throw in javascript, it returns undefined. If you want to throw an error, just throw it: `throw new Error()`. – ray Sep 28 '21 at 04:58
  • I'm sure you found this already, but if you haven't, here it is... https://stackoverflow.com/questions/6728424/array-out-of-bounds-comparison-with-undefined-or-length-check – Adriano Sep 28 '21 at 04:59
  • But doesn't attributing the length of an array for a for loops index cause an error instead of just returning undefined? I can see strArr[10] returning undefined, but what about within the loop statement? – twominds Sep 28 '21 at 04:59
  • Not sure I understand the question. You can access `strArr[9999999]` and you'll get `undefined` without throwing any errors. The middle bit of the for loop is just a condition. If it's true, the loop continues, if it's false, the loop is done. – ray Sep 28 '21 at 05:01
  • Yes, you unfortunately don't understand the question. I am not talking about accessing a value within an index, I am talking about utilizing an arrays length for the statement within a for loop and causing the catch to execute. – twominds Sep 28 '21 at 05:03
  • It's already stated, no error occurs. You can put a condition inside the loop, and throw the error if the condition passes. – Teemu Sep 28 '21 at 05:05
  • I'm not sure the condition you're attempting to trigger actually exists in javascript. You could get an error by accessing `strArr.foo.bar`, but it's not going to be a range/bounds error. – ray Sep 28 '21 at 05:05
  • I am not trying to throw an error under a known condition. It shall only throw an error if the loops index goes out of bounds. – twominds Sep 28 '21 at 05:05
  • @rayhatfield you are still talking about accessing values that don't exist through dot notation or indexing. I am talking about a for loops statement causing the catch to execute. – twominds Sep 28 '21 at 05:07
  • Again: JS doesn't throw an error when referring outbounded index, there's no automatic for this, you've to check it manually. – Teemu Sep 28 '21 at 05:07
  • Please refer to the updated information, I am a student so I am learning the ins and outs of JS. – twominds Sep 28 '21 at 05:10
  • 2
    "_This will cause an array out of bounds error_" is just wrong, it won't. – Teemu Sep 28 '21 at 05:12
  • @Teemu This is my problem, is that Im being told one thing by a University, and then not being able to recreate the same issue that they want. I agree with you Teemu, Im just confused on what they want. – twominds Sep 28 '21 at 05:13
  • I guess I can just put a throw "error" inside the for loop and call it a day? – twominds Sep 28 '21 at 05:15
  • 2
    And this is a javascript course? Not a java course? – ray Sep 28 '21 at 05:15
  • This is a course that encapsulates many types of languages. I am chore'd with JS and PHP. Thank you guys for your help, I will just put a throw error inside my loop and hope my professor doesn't say anything. – twominds Sep 28 '21 at 05:16
  • @rayhatfield Could this error be caused by a for loop in Java? – twominds Sep 28 '21 at 05:24
  • Not “by a for loop”, but attempting to access an index beyond the array bounds will throw an [IndexOutOfBoundsException](https://docs.oracle.com/javase/7/docs/api/java/lang/IndexOutOfBoundsException.html) in java, and it’s common in other languages. The directions you posted indicate that accessing `strArray[strArray.length]` will throw and that’s simply not true in javascript. – ray Sep 28 '21 at 05:29
  • @rayhatfield Thank you for correcting my misunderstanding, maybe this question will be useful for someone's else with similar misconceptions. – twominds Sep 28 '21 at 05:30

0 Answers0