
- 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
Are mean mode of a dataset equal in JavaScript
We are required to write a JavaScript function that takes in an array of sorted numbers. The function should calculate the mean and the mode of the dataset. Then if the mean and mode are equal, the function should return true, false otherwise.
For example −
If the input array is −
const arr = [5, 3, 3, 3, 1];
Then the output for this array should be true because both the mean and median of this array are 3.
Example
Following is the code −
const arr = [5, 3, 3, 3, 1]; mean = arr => (arr.reduce((a, b) => a + b))/(arr.length); mode = arr => { let obj = {}, max = 1, mode; for (let i of arr) { obj[i] = obj[i] || 0; obj[i]++ } for (let i in obj) { if (obj.hasOwnProperty(i)) { if ( obj[i] > max ) { max = obj[i] mode = i; } } } return +mode; } const meanMode = arr => mean(arr) === mode(arr) console.log(meanMode(arr));
Output
Following is the output on console −
true
- Related Articles
- Mean and Mode in SQL Server
- Commands to Get Min, Max, Median, and Mean of a Dataset
- What do you mean by Mode?
- What Is Mean, Median, Mode , and Range of a data set?
- Application of Mean, Median, and Mode of Grouped Data in Psychology
- Finding Mean, Median, Mode in Python without Libraries
- Are array of numbers equal - JavaScript
- The marks in science of 13 students are 31,37,29,41,35,35,38,36,35,38,32,29,43 . Find the range, Median , Mean and Mode.
- What are the characteristics of JavaScript 'Strict Mode'?
- Find the mean, mode and median of the following frequency distribution:
- Finding Mode in a Binary Search Tree in JavaScript
- Check if some elements of array are equal JavaScript
- What is Strict mode in JavaScript?
- Check if values of two arrays are the same/equal in JavaScript
- In a mathematics test given to 15 students, the following marks (out of 100 ) are recorded:$41,39,48,52,46,62,54,40,96,52,98,40,42,52,60$Find the mean, median and mode of this data.

Advertisements