}, //closes the clamp object
//step 7 - 10
inRange(number, start, end){
if(end == 'undefined'){
end = start
start = 0
}
if(start > end){
let temp = end
end = start
start = temp
}
// suggestion from lindebrand below
const isInRange = start <= number && number < end
return isInRange
} //closes the inRange object
Here are the test results from running > node test/inRange.js. You can see that 5- isn’t happy. What have I done wrong?
$ node test/in-range.js
_.inRange() Tests:
1 - _.inRange() is defined - Passed!
2 - Returns true if an in-range value is in range - Passed!
3 - Returns false if a too small value is out of range - Pass
ed!
4 - Returns false if a too large value is out of range - Pass
ed!
5 - Uses end value as start value and start value as 0 if end
value is not defined - Failed: _.inRange(1, 2) returned fals
e instead of true.
6 - Reverses start and end values if start is bigger than end
- Passed!
$
Found the problem. Changed this:
if(end == ‘undefined’)
to:
if(end === undefined)
All guns are firing now. Sorry to bug you.
$ node test/in-range.js
_.inRange() Tests:
1 - _.inRange() is defined - Passed!
2 - Returns true if an in-range value is in range - Passed!
3 - Returns false if a too small value is out of range - Pass
ed!
4 - Returns false if a too large value is out of range - Pass
ed!
5 - Uses end value as start value and start value as 0 if end
value is not defined - Passed!
6 - Reverses start and end values if start is bigger than end
- Passed!
$
inRange(number,start,end) {
let first = “”;
let second = “”;
let third = “”;
if (!end) {
first = number;
second = 0;
third = start; }
else if (start > end) {
first = number;
second = end;
third = start;
} else {
first = number;
second = start;
third = end;
};
if (second <= first && first < end) {
return true } else {
return false
};
},
Intermediary variables are sometimes necessary to help with steps, but when they are not needed it makes no sense to bring them in. Take for instance,
if start > end
temp = start
start = end
end = temp
temp is an intermediary variable that has use and meaning. In JS we cannot swap two values without a temporary variable.
first = number
Why do we need to store number since it does not change? Once we swap start and end as needed, why do we need to store them in different variables? There is no good answer for these questions, the detail that explains why, “All the extra variables are only confusing the situation.”