Search code examples
javascriptarraysreactjsalgorithmquicksort

TypeError: Cannot read property 'style' of undefined error


I am working on animating a quicksort algorithm visualiser in React. While my code is not final and I am still working on it, I am getting a 'TypeError: Cannot read property 'style' of undefined' error. I am learning React with this project but can't figure out what is causing this error. The error comes from this line: const barOneStyle = arrayBars[barOneIndex].style;

quickSort() {
        const animations = getQuickSortAnimations(this.state.array);
        for (let i = 0; i < animations.length; i++) {
            const isColorChange = animations[i][0] === "comparision1" || animations[i][0] === "comparision2";
            const arrayBars = document.getElementsByClassName('array-bar');
            if(isColorChange === true) {
                const [comparision, barOneIndex, barTwoIndex] = animations[i];
                const color = (animations[i][0] === "comparision1") ? 'navy' : 'pink';
                const barOneStyle = arrayBars[barOneIndex].style;
                const barTwoStyle = arrayBars[barTwoIndex].style;
                setTimeout(() => {
                    barOneStyle.backgroundColor = color;
                    barTwoStyle.backgroundColor = color;
                },i * ANIMATION_SPEED_MS);
            } 
            else {
                const [barIndex, newHeight] = animations[i];
                if (barIndex === -1) {
                    continue;
                }
                const barStyle = arrayBars[barIndex].style;
                setTimeout(() => {
                    barStyle.height = `${newHeight}px`;
                },i * ANIMATION_SPEED_MS);  
            }        }
        //this.setState({array: sortArray})
        //const RESTORE_TIME = parseInt(ANIMATION_SPEED_MS*animations.length/2 + 3000);
        //setTimeout(() => this.restoreStoreButtons(), RESTORE_TIME);  
        }

I did not get this error for the mergesort in the same project:

mergeSort() {
        const animations = getMergeSortAnimations(this.state.array);
        for (let i= 0; i < animations.length; i++) {
            const arrayBars = document.getElementsByClassName('array-bar');
            const isColorChange = i % 3 !== 2;
            if (isColorChange) {
                const [barOneIdx, barTwoIdx] = animations[i];
                const barOneStyle = arrayBars[barOneIdx].style;
                const barTwoStyle = arrayBars[barTwoIdx].style;
                const color = i% 3 === 0 ? 'navy' : 'pink';
                setTimeout(() => {
                    barOneStyle.backgroundColor = color;
                    barTwoStyle.backgroundColor = color; 
                }, i * ANIMATION_SPEED_MS);
            } else {
                setTimeout(() => {
                    const [barOneIdx, newHeight] = animations[i];
                    const barOneStyle = arrayBars[barOneIdx].style;
                    barOneStyle.height = `${newHeight}px`;
                }, i * ANIMATION_SPEED_MS);
            }
        }
    
    }

Solution

  • Try this

    quickSort() {
            const animations = getQuickSortAnimations(this.state.array);
            for (let i = 0; i < animations.length; i++) {
                const isColorChange = animations[i][0] === "comparision1" || animations[i][0] === "comparision2";
                const arrayBars = document.getElementsByClassName('array-bar');
                if(isColorChange === true) {
                    const [comparision, barOneIndex, barTwoIndex] = animations[i];
                    const color = (animations[i][0] === "comparision1") ? 'navy' : 'pink';
                    const barOneStyle = arrayBars[barOneIndex]?arrayBars[barOneIndex].style: {};
                    const barTwoStyle = arrayBars[barTwoIndex]?arrayBars[barTwoIndex].style:{};
                    setTimeout(() => {
                        barOneStyle.backgroundColor = color;
                        barTwoStyle.backgroundColor = color;
                    },i * ANIMATION_SPEED_MS);
                } 
                else {
                    const [barIndex, newHeight] = animations[i];
                    if (barIndex === -1) {
                        continue;
                    }
                    const barStyle = arrayBars[barIndex]?arrayBars[barIndex].style:{};
                    setTimeout(() => {
                        barStyle.height = `${newHeight}px`;
                    },i * ANIMATION_SPEED_MS);  
                }        } 
            }