
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Calculating average of a sliding window in JavaScript
We are required to write a JavaScript function that takes in an array of integers, arr, as the first argument and a number, num (num < length of arr) as the second argument. The function should construct and return a new array that contains the average of all possible num contiguous numbers of the array.
For example −
If the input array and the number are −
const arr = [1, 2, 3, 4, 5]; const num = 2;
Then the output should be −
const output = [1.5, 2.5, 3.5, 4.5];
because the possible continuous windows of size two are (1, 2), (2, 3), (3, 4) and (4, 5)
Example
The code for this will be −
const arr = [1, 2, 3, 4, 5]; const num = 2; const findContiniousAverage = (arr = [], num = 1) => { if(num > arr.length){ return []; }; const res = []; let sum = 0; let left = 0, right = 0; for(; right < num; right++){ sum += arr[right]; }; res.push(sum / num); for(; right < arr.length; right++, left++){ sum -= arr[left]; sum += arr[right]; res.push(sum / num); }; return res; }; console.log(findContiniousAverage(arr, num)); console.log(findContiniousAverage(arr));
Output
And the output in the console will be −
[ 1.5, 2.5, 3.5, 4.5 ] [ 1, 2, 3, 4, 5 ]
- Related Articles
- Calculating excluded average - JavaScript
- Calculating average of an array in JavaScript
- Sliding Window Protocol
- Calculating quarterly and yearly average through JavaScript
- Sliding Window Maximum in C++
- Sliding Window Median in C++
- A One-Bit Sliding Window Protocol
- Sliding Window Protocol (Selective Repeat)
- What is a Sliding Window Protocol in Computer Network?
- Find all subarrays with sum equal to number? JavaScript (Sliding Window Algorithm)
- What is three-bit sliding window protocol?
- Calculating h index of a citation in JavaScript
- Calculating the weight of a string in JavaScript
- Calculating average value per document with sort in MongoDB?
- Calculating least common of a range JavaScript

Advertisements