Find smallest number
JavaScript
easy
10 mins
Given an array arr of numbers, return the smallest number in the array. If the array is empty, return null.
Examples
Input: arr = [3, 1, 2] Output: 1
Input: arr = [-5, 2, -3, 4] Output: -5
Input: arr = [0, 2, 3] Output: 0
Input: arr = [] Output: null
Constraints
- Input must be an array of finite numbers
- Return false for non-array inputs
- Return false for arrays containing non-number values
- Return false for arrays containing NaN, Infinity, or -Infinity
- For an empty array, return null
Function Signature
function findSmallest(arr) { // Your code here }
Test Cases
- Base cases: empty array [] -> null, single [ x ] -> x
- Mixed arrays: [ 3, 1, 2 ], [ -5, 2, -3, 4 ]
- All negatives: [ -1, -2, -3 ]
- All positives: [ 1, 2, 3 ]
- Decimals: [ -1.5, -0.1, 0, 2.2 ]
- Invalid inputs: null, undefined, 42, '8', {}, () => {}, [1, 'a'], [NaN], [Infinity]
Companies:
Accenture
capgemini
tcs
infosys
wipro
tech mahindra
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
