-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathsmallest.js
More file actions
40 lines (36 loc) · 836 Bytes
/
smallest.js
File metadata and controls
40 lines (36 loc) · 836 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Return the smallest number in an array of numbers.
*
* @param {number[]} arr - The input array of numbers
* @returns {number} - Returns smallest number
*
* ex: smallest([3, 0])
* returns 0
*
* ex: smallest([1, -1, 1, 1, 1])
* returns -1
*
* ex: smallest([1, 1, 2])
* returns 1 (does not matter if it is the first or second 1)
*/
function smallest(arr) {
let l = Infinity //store largest
let l2 = Infinity //store second largest
for (let i = 0 ; i < arr.length; i ++) {
if (arr[i] < l) { //if 5 is greater than - infinity
l = arr[i] // l becomes 5
l2 = l
} else if (arr[i] < l2) {
l2 = arr[i]
}
}
return l2
}
module.exports = smallest
// let small = arr.reduce((acc,currentEl) =>{
// if(acc < currentEl) {
// return acc
// } else {
// return currentEl
// }
// })