- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 smallest number using recursion in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers and returns the smallest number from it using recursion.
Let’s say the following are our arrays −
const arr1 = [-2,-3,-4,-5,-6,-7,-8]; const arr2 = [-2, 5, 3, 0];
The code for this will be −
const arr1 = [-2,-3,-4,-5,-6,-7,-8]; const arr2 = [-2, 5, 3, 0]; const min = arr => { const helper = (a, ...res) => { if (!res.length){ return a; }; if (a < res[0]){ res[0] = a; }; return helper(...res); }; return helper(...arr); } console.log(min(arr1)); console.log(min(arr2));
Following is the output on console −
-8 -2
- Related Articles
- JavaScript Recursion finding the smallest number?
- Finding the smallest fitting number in JavaScript
- Finding product of an array using recursion in JavaScript
- Finding smallest number that satisfies some conditions in JavaScript
- Finding the greatest and smallest number in a space separated string of numbers using JavaScript
- Finding greatest digit by recursion - JavaScript
- Finding the smallest multiple in JavaScript
- Finding difference of greatest and the smallest digit in a number - JavaScript
- Finding a number of pairs from arrays with smallest sums in JavaScript
- Finding the smallest good base in JavaScript
- Third smallest number in an array using JavaScript
- Finding second smallest word in a string - JavaScript
- Finding smallest sum after making transformations in JavaScript
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- Finding the smallest value in a JSON object in JavaScript

Advertisements