
- 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
How to get the numbers which can divide all values in an array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns a number which can exactly divide all the numbers in the array.
Let’s say the following is our array −
const arr = [4, 6, 34, 76, 78, 44, 34, 26, 88, 76, 42];
Example
Following is the code −
const arr = [4, 6, 34, 76, 78, 44, 34, 26, 88, 76, 42]; const dividesAll = el => { const result = []; let num; for (num = Math.floor(el / 2); num > 1; num--){ if (el % num === 0) { result.push(num); } }; return result; }; const dividesArray = arr => { return arr.map(dividesAll).reduce((acc, val) => { return acc.filter(el => val.includes(el)); }); }; console.log(dividesArray(arr));
Output
This will produce the following output in console −
[ 2 ]
- Related Articles
- How to get all unique values in a JavaScript array?
- How to get the first n values of an array in JavaScript?
- JavaScript How to get all 'name' values in a JSON array?
- How to get the most common values in array: JavaScript ?
- Get the max n values from an array in JavaScript
- Summing all the unique values of an array - JavaScript
- Product of all other numbers an array in JavaScript
- Sum of all prime numbers in an array - JavaScript
- How to get the values of an object in JavaScript?
- All ways to divide array of strings into parts in JavaScript
- How can I split an array of Numbers to individual digits in JavaScript?
- How to get all docs which contain another doc in an array with MongoDB?
- How to get the sum of the powers of all numbers in JavaScript?
- Get the number of true/false values in an array using JavaScript?
- Get only specific values in an array of objects in JavaScript?

Advertisements