-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathsmallest.js
43 lines (36 loc) · 1.11 KB
/
smallest.js
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
41
42
43
/* eslint-disable no-self-assign */
/* eslint-disable no-use-before-define */
/**
* Given an array, returns the smallest element in the array.
*
* Assume the array contains only numbers.
*
* @param {number[]} array - The input array
* @returns {number} The smallest element in the array
*/
function smallest(array) {
// The _____ on each of the lines below are meant to act
// like a fill-in-the-blank so you can see the structure.
//
// Replace each _____ with some code (not all blanks) will
// use the same code.
let _____ = _____;
for (let _____ of _____) {
if (_____) {
_____ = _____;
}
}
return _____;
}
if (require.main === module) {
console.log('Running sanity checks for smallest:');
console.log(smallest([1, 2, 3]) === 1);
console.log(smallest([0, -100, 50, -200]) === -200);
console.log(smallest([-200, -400, -100, -300]) === -400);
console.log(smallest([0]) === 0);
console.log(smallest([1]) === 1);
console.log(smallest([-1]) === -1);
console.log(smallest([11, 11, 11]) === 11);
console.log(smallest([-22, -11, -22]) === -22);
}
module.exports = smallest;