Problem overview
The method minValue receives three numbers, A, B and C, and returns the smallest of them. The lesson forbids built-in helpers such as Math.min, so the comparison has to be written by hand.
For A = 7, B = 4, C = 9 the answer is 4. For A = 3, B = 8, C = 3 the answer is 3. The second example shows that the smallest value may appear twice, and the method still returns it once.
Approach: keep the best answer found so far
One way to solve this is to ask, for each value, whether it is less than or equal to both of the others. If A passes that test, return it. If not, test B. If neither passes, C must be the smallest. That works, but the conditions get longer with every value you add.
This lesson uses a scalable idea instead. Keep a variable minVal that holds the smallest value seen so far. Start it at A, because with only one value looked at, that value is the minimum. Then look at each remaining value in turn. If it is smaller than minVal, it replaces it. When every value has been looked at, minVal is the answer.
Here is the idea on 7, 4, 9. minVal starts as 7. Since 4 is less than 7, minVal becomes 4. Since 9 is not less than 4, minVal stays 4. The method returns 4.
The same pattern handles an array of any length: replace the two if statements with a loop. Later lessons rely on it.
Step by step
- Declare
minValwithlet, typednumber, and set it toA. Useletbecause the variable will change. Aconstwould make the later assignments a compile error. - Write an
ifthat checks whetherBis less thanminVal. It starts empty so you can focus on the condition. - Inside it, assign
BtominVal. The comparison is strict (<), because an equal value changes nothing. - Add a second, separate
ifforC. It must not be anelseof the first one.Chas to be compared whether or notBtook over, sinceCmay beat both. - Return
minVal. After both checks, it holds the smallest of the three.
The solution in TypeScript
class Solution {
minValue(A: number, B: number, C: number): number {
let minVal: number = A;
if (B < minVal) {
minVal = B;
}
if (C < minVal) {
minVal = C;
}
return minVal;
}
}
Some details worth knowing:
numbercovers every numeric value. TypeScript has one number type for integers and decimals, so the method works for-1.5or0.25as well as for whole numbers.- The annotation on
minValis optional. TypeScript infersnumberfromA. Writing it out is just a way of being explicit. - Types disappear at run time. The compiler checks that you pass numbers, but the generated JavaScript has no such check. A caller from plain JavaScript could still pass strings, and then
<would compare them as text.
Edge cases and common mistakes
- Using
else iffor the second check. IfBreplacedminVal, theCcheck would be skipped.minValue(5, 3, 1)would then return 3 instead of 1. - Starting
minValat 0. The result would be wrong whenever all three values are positive, because 0 would beat them all. Start from a real input value. - All negative numbers.
minValue(-1, -5, -3)returns -5. The comparisons work the same way for negatives. - Equal values. With
3, 8, 3, nothing is smaller than 3, sominValstays atAand the result is 3.
Testing
minValue(7, 4, 9)gives 4.minValue(3, 8, 3)gives 3.minValue(-1, -5, -3)gives -5.