Lodash project: question 19

Hello Everyone, Happy New Year!! :partying_face:

I’m working on the Lodash project, specifically question 19 - 22.

I reviewed this post from back in Oct and as a result, of some changes, here is my current code:

  }, //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!
$

Any insight is appreciated. Thank you.

1 Like

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!
$
3 Likes

That is a function that needs to be invoked.

2 Likes

So what’s wrong is this? :expressionless:

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
};
},

Hi ahmetcank, Make sure you add a link to the lesson you are working on.

You mind find some helpful tips on my earlier post.

1 Like

We shouldn’t need any more working variables. number, start and end are all we need.

If there is no end value, then we set it to the value of start and set start to zero. All the extra variables are only confusing the situation.

If start is greater than end then we simply swap the two, using a temporary variable to hold the value first being overwritten.

We wish to determine if number is between start (inclusive) and end (exclusive).

See if you can rewrite your code using only the three variables in the parameter list.

1 Like

Thanks, I fixed my code. works well :grin:

Could you give me more details about why " All the extra variables are only confusing the situation. "

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.”

1 Like