Skip to content
Advertisement

How can I copy the elements on an array into another array with a different size?

I’m working on a project with arrays. I’m using a method that adds values to an array of size 20. I’m supposed to be able to change the size of the array while also transferring values from the previous array into the new array. I used Array.copyOfRange which should work but for some reason when I run the code and try changing the size of the array. I get an Error Message that says Index out of bounds. Could someone help me figure out why it would say that when this should work?

JavaScript

Advertisement

Answer

Because in your changeCapacity function you are not updating the capacity field with the newCapacity being passed.

JavaScript

Just replace your changeCapacity function with above code and it should work fine.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement