.inRange() Lodash: which one is the right solution?

Hi there, I’m dealing with this Exercise

I solved it with this code, which appeared to be quite right to me. But the terminal didn’t think so apparently.

inRange(number, start, end) {
    if (number < start) {
     return false;
   } if (number >= end) {
     return false;
   } if (number >= start && number < end) {
     return true;
   } if (!end) {
     end = start;
     start = 0;
   } if (start > end) {
     var temp = end;
     end = start;
     start = temp;
   }
 },
};

Then after a huge amount of thoughts I checked out the video guide and apparently I just had to change the order like this:

inRange(number, start, end) {
 if (!end) {
     end = start;
     start = 0;
   } if (start > end) {
     var temp = end;
     end = start;
     start = temp;
   } if (number < start) {
     return false;
   } if (number >= end) {
     return false;
   } if (number >= start && number < end) {
     return true;
   }
 },
};

Can anyone explain me what’s the real reason why the terminal didn’t agree with the first code?

Thanks

Well the check whether there is an end defined and the end-start switcher are rather significant and have to be run before the rest of the operations. So those have to be at the start of the inRange method.

Thank you, it makes more sense to me now.

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.