
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding the smallest fitting number in 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.
Therefore, let’s write the code for this function −
Example
The code for this will be −
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
The output in the console will be −
[ 2 ]
- Related Questions & Answers
- JavaScript Recursion finding the smallest number?
- Finding smallest number using recursion in JavaScript
- Finding the smallest multiple in JavaScript
- Finding smallest number that satisfies some conditions in JavaScript
- Finding the smallest good base in JavaScript
- Finding difference of greatest and the smallest digit in a number - JavaScript
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- Finding a number of pairs from arrays with smallest sums in JavaScript
- Finding the smallest value in a JSON object in JavaScript
- Finding second smallest word in a string - JavaScript
- Finding smallest sum after making transformations in JavaScript
- Finding the greatest and smallest number in a space separated string of numbers using JavaScript
- Finding out the Harshad number JavaScript
- Finding the nth prime number in JavaScript
- Finding the smallest positive integer not present in an array in JavaScript
Advertisements