I have a program (incomplete) that generates 100 divs, each with class "bar", and each with a random div.height property. Imagine a histogram if you need a visual where each bar has a random height.
I store the list of bars in a constant using:
const arrayBars = document.getElementsByClassName('bar');
Everything so far is fine.
What is strange is that, when I create two new constants (barA and barB) and try to console.log their height, they ALWAYS come back with the same height which should not be the case. I am certain that the problem is not in my random div.height generator as they work as expected in other parts of my program.
I believe the problem lies in this part of my program:
function insertionSort(unsortedArray) {
let length = unsortedArray.length;
for (let i = 1; i < length; i++) {
const arrayBars = document.getElementsByClassName('bar');
const barA = arrayBars[i].style;
let j = i - 1;
const barB = arrayBars[j + 1].style;
console.log(barA.height, barB.height);
}
}
When using the debugger, I noticed that the point in time in which barB's height becomes that of barA is for the line:
const barB = arrayBars[j + 1].style;
I feel like this is a referencing problem but nowhere in the line above do I ever set barB to refer to barA. And it can't be the fact that barB calls arrayBars, right?
In that case, why do barA and barB always have the same height?