Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Average Of Numbers

const average = (...args) => args.reduce((a, b) => a + b) / args.length;

average(1, 2, 3, 4);
// Result: 2.5
Comment

average value


// calculate the average value for binary tree 
const AvgValue = (root) => {
  let result = [];
  let avg = 0;
  let sum = 0;
  let queue = [root];
  while (queue.length) {
    let current = queue.shift();
    result.push(current.data);
    if (current.left) queue.push(current.left);
    if (current.right) queue.push(current.right);
  }
  for (let i = 0; i < result.length; i++) {
    sum = sum + result[i];
  }
  avg = Math.floor(sum / result.length);
  return avg;
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: http module in nodejs 
Javascript :: mac os chrome opne debug new tab 
Javascript :: how to assert in javascript 
Javascript :: delete element javascript 
Javascript :: jquery validate array input not working 
Javascript :: vue 3 hooks 
Javascript :: reverse integer in javascript 
Javascript :: zustand simple counter 
Javascript :: what is xhr 
Javascript :: angular implementing Validator 
Javascript :: dropzone add download button addedfile 
Javascript :: How do I redirect to another webpage using javascript 
Javascript :: jquery change h1 text 
Javascript :: get url of website javascript 
Javascript :: model nodejs 
Javascript :: Add an item to the beginning of an Array 
Javascript :: how to check if a date has passed javascript 
Javascript :: js binary 
Javascript :: use $ instead of jQuery 
Javascript :: background image not loading from a link react 
Javascript :: react html parser 
Javascript :: accept only video in input type file below size 
Javascript :: convert string to camelcase 
Javascript :: iife in javascript 
Javascript :: innerhtml 
Javascript :: check valid Phonenumbers 
Javascript :: nvalid response body while trying to fetch https://registry.npmjs.org/scheduler: Socket timeout 
Javascript :: js some array 
Javascript :: ${} js 
Javascript :: javascript null check 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =