You are trying to iterate the array while keeping track of the index of its elements.
In your code, you used the for (let x in arr) method to iterate the array. This method is used to iterate over values of properties in an object. Rather use for let x of arr and note that x in this case is the element itself not its index. I will suggest you use a for loop to iterate the array.
Or
for(let [i, val] of arr.entries()) {
// i is the index and val is the value at that index
}