Created
February 23, 2026 13:52
-
-
Save krakowdeveloper/79aafdd60ad3666c50bb37c9ef351ec6 to your computer and use it in GitHub Desktop.
slide_window_practice _ google.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Solution(arr, size){ | |
| this.maxVal = 0; | |
| let sum = 0; | |
| let storage = []; // muestra valores calculados | |
| let len = arr.length; | |
| //1. lets make sure it reads the complete values: | |
| if (arr.length < size) anulate() | |
| function anulate(){ | |
| throw new Error("slide window size is bigger than the current data") | |
| } | |
| this.add = function(x){ | |
| //suma la primer ventana | |
| for(let i = x; i < x + size; i++){ | |
| sum += arr[i]; | |
| } | |
| storage.push(sum); | |
| sum = 0; //reinicio el calculo para que no se aumente | |
| } | |
| this.calculate = function(){ | |
| //guardar como maxima suma: | |
| const len = storage.length; | |
| if(len > 0){ | |
| this.maxVal = Math.max(...storage); | |
| } | |
| } | |
| this.slide = function(){ | |
| //mueve la ventana | |
| for(let i = 0; i <= len - size; i++){ | |
| this.add(i); | |
| } | |
| } | |
| this.cache = function(){ | |
| return storage; | |
| } | |
| this.result = function(){ | |
| this.slide() | |
| this.calculate(); | |
| return this.maxVal; | |
| } | |
| } | |
| const vector1 = new Solution([1,3,4,6,7], 3) | |
| console.log(vector1.result(), "cache:", vector1.cache()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment