Hard
How to Process Time Series Data with Moving Average and Exponential Smoothing in JavaScript
Your team's data visualization dashboard needs to process time series data. What will be the final transformed output of this data processor?
1class TimeSeriesProcessor {2 static #SMOOTHING_FACTOR = 0.15;34 static movingAverage(values, windowSize) {5 const result = [];6 for (let i = 0; i < values.length; i++) {7 let sum = 0;8 let count = 0;9 for (let j = Math.max(0, i - windowSize + 1); j <= i; j++) {10 sum += values[j];11 count++;12 }13 result.push(Number((sum / count).toFixed(2)));14 }15 return result;16 }1718 static exponentialSmoothing(values) {19 const result = [values[0]];20 for (let i = 1; i < values.length; i++) {21 const smoothed = this.#SMOOTHING_FACTOR * values[i] +22 (1 - this.#SMOOTHING_FACTOR) * result[i - 1];23 result.push(Number(smoothed.toFixed(2)));24 }25 return result;26 }2728 static process(data, windowSize = 3) {29 const movingAvg = this.movingAverage(data, windowSize);30 return this.exponentialSmoothing(movingAvg);31 }32}3334const data = [10, 15, 12, 18, 20];35console.log(TimeSeriesProcessor.process(data));