- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Third smallest number in an array using JavaScript
Problem
We are required to write a JavaScript function that takes in an array of numbers of length at least three.
Our function should simply return the third smallest number from the array.
Example
Following is the code −
const arr = [6, 7, 3, 8, 2, 9, 4, 5]; const thirdSmallest = () => { const copy = arr.slice(); for(let i = 0; i < 2; i++){ const minIndex = copy.indexOf(Math.min(...copy)); copy.splice(minIndex, 1); }; return Math.min(...copy); }; console.log(thirdSmallest(arr));
Output
4
Advertisements