-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Open
Description
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.
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;
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels