-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathsmallest.js
More file actions
32 lines (26 loc) · 1008 Bytes
/
smallest.js
File metadata and controls
32 lines (26 loc) · 1008 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
/**
* 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 smallestNum = arr[0] // create a new variable to store smallest number
// set smallest number to array at index zero (for starting point)
for(let i = 0; i <= arr.length - 1; i++) // create a loop to iterate through array of numbers
if(arr[i] < smallestNum) // if the number at index is smaller than smallest number
smallestNum = arr[i] // that will become our new smallest number
return smallestNum
}
console.log(smallest([3, 0]))
console.log(smallest([1, 1, 2]))
module.exports = smallest