Skip to content

Bug Report for moving-average-from-data-stream #5324

@AbisoyeAlli

Description

@AbisoyeAlli

Bug Report for https://neetcode.io/problems/moving-average-from-data-stream

For the first JavaScript solution provided (see screenshot below), the solution tries to access methods directly without using this keyword which causes a runtime error.

Image

current code:

next(val) {
        const size = this.size;
        const queue = this.queue;

        queue.push(val);

        // calculate the sum of the moving window
        const windowSum = queue.slice(-size).reduce((sum, num) => sum + num, 0);

        return windowSum / Math.min(queue.length, size);
    }

it should be this, removed the this.size = this.size; and this.queue = this.queue; as they are redundant assignments, we are assigning the property to themselves:

next(val) {
    
        this.queue.push(val);

        const windowSum = this.queue.slice(-this.size).reduce((sum, num) => sum + num, 0);

        const sumAvg = windowSum / Math.min(this.queue.length, this.size);

        return sumAvg;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions