Overview
Implement inRange(value, start, end). If end is omitted, set end=start, start=0. Range is [start, end). If start > end, swap.
Examples
inRange(3, 2, 4); // true inRange(4, 8); // true inRange(-3, -2, -6); // true
Solution
Reveal solution
function inRange(value, start, end) {
if (end === undefined) { end = start; start = 0; }
if (start > end) { const t = start; start = end; end = t; }
return value >= start && value < end;
}in-range.js
In Range
easycodingJavaScriptMath
Overview
Implement inRange(value, start, end). If end is omitted, set end=start, start=0. Range is [start, end). If start > end, swap.
Examples
inRange(3, 2, 4); // true inRange(4, 8); // true inRange(-3, -2, -6); // true
Solution
Reveal solution
function inRange(value, start, end) {
if (end === undefined) { end = start; start = 0; }
if (start > end) { const t = start; start = end; end = t; }
return value >= start && value < end;
}NameTopicDifficulty
103 of 103 problems