简体   繁体   中英

Why min and max range method does not return valid output

I need to have a method which needs to check in the valid range between -064.000000 to -180.000000 and 142.000000 to 180.000000 . The ranges object that I have looks like the following:

"ranges": {
  "range1": {
    "min": -180,
    "max": -64
  },
  "range2": {
    "min": 142,
    "max": 180
  }
}

So far, this is what I was able to complete but it doesn't seem to work right:

const mustBeInRangeInclusive = ({ userInput, ranges }) => {
  let data_num = _.toNumber(userInput);
  for (let i = 0; i < ranges.length; i++) {
    return result = data_num >= ranges[i][0] && data_num <= ranges[i][1];
  }
};

Can someone please help me complete this method to figure out what am I doing wrong?

Expected output:

-63 -> invalid
-64: valid
181 -> invalid
180: valid
141 -> invalid
142: valid

Few edits, as the question code keeps changing a bit.

First problem - accessing objects properties

If you have an array , you access its values by indeces.

let array = [1, 2, 3];
let firstItem = array[0];
let secondItem = array[1];

If you have object, you access its propeties by their names.

let someObject = { 'name': 'John', 'age': 21 };
let name = someObject.name;
name = someObject['name'];

If you have array of objects, you combine both methods.

let arrayOfObjects = [
    { 'name': 'John', 'age': 21 },
    { 'name': 'Sam', 'age': 23 }
]
let firstObjectsName = arrayOfObjects[0].name;

Second problem - exiting the loop on the first iteration You call return statement as soon as you enter the loop making it impossible to enter the second iteration. You could store result of each iteration in the array and return it in the end.

const mustBeInRangeInclusive = ({ userInput, ranges }) => {
  let results = [];
  let data_num = _.toNumber(userInput);
  for (let i = 0; i < ranges.length; i++) {
    results.push(data_num >= ranges[i].min && data_num <= ranges[i].max);
  }

  return results;
};

This answer based upon the comment request of OP, and does not solve the issue if filters would be an object.

Assuming you can change the definition, using an array of filters would be a lot easier to work with than an object with filters. You can use the array every method to check if every elements matches a criteria. Use some to check if some (one or more) elements matches a criteria.

 const userInput = document.getElementById("user-input"); const ranges = [ // <- changed definition to an array { "min": 0, "max": 100 }, { "min": 50, "max": 150 }, ]; userInput.addEventListener("change", () => { const nr = parseInt(userInput.value, 10); const coversNr = ({min, max}) => min <= nr && nr <= max; const withinEveryRange = ranges.every(coversNr); const withinSomeRange = ranges.some(coversNr); console.log("withinEveryRange //=>", withinEveryRange); console.log("withinSomeRange //=>", withinSomeRange ); });
 <input id="user-input" type="number" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM