简体   繁体   English

为什么最小和最大范围方法不返回有效的 output

[英]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 .我需要一种方法来检查-064.000000 to -180.000000142.000000 to 180.000000之间的有效范围。 The ranges object that I have looks like the following:我拥有的范围 object 如下所示:

"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:预期 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.如果您有 object,您可以通过其名称访问其属性。

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.第二个问题 - 在第一次迭代时退出循环进入循环后立即调用return语句,从而无法进入第二次迭代。 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.此答案基于 OP 的评论请求,如果filters是 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.假设您可以更改定义,使用过滤器数组将比使用过滤器的 object 更容易使用。 You can use the array every method to check if every elements matches a criteria.您可以使用数组every方法来检查每个元素是否符合条件。 Use some to check if some (one or more) elements matches a criteria.使用some检查某些(一个或多个)元素是否符合条件。

 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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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