Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查找数组中的数字是否介于两个数字之间,并输出介于两个数字之间的值的数目_Javascript_Arrays - Fatal编程技术网

Javascript 查找数组中的数字是否介于两个数字之间,并输出介于两个数字之间的值的数目

Javascript 查找数组中的数字是否介于两个数字之间,并输出介于两个数字之间的值的数目,javascript,arrays,Javascript,Arrays,在过去的几个小时里,我一直试图找出如何在数组中找到介于2个数字之间的数字,但我不知道该去哪里。我需要做什么?18和20只是占位符数字,您可以随意使用任何数字 function start() { var array = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20]; var lower = 18; var upper = 20; if (need help here) { alert('Yes'); }

在过去的几个小时里,我一直试图找出如何在数组中找到介于2个数字之间的数字,但我不知道该去哪里。我需要做什么?18和20只是占位符数字,您可以随意使用任何数字

function start() {
    var array = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20];
    var lower = 18;
    var upper = 20;

    if (need help here) {
        alert('Yes');
    } else {
        alert('No');
    }

    document.getElementById('listOfvalues').innerHTML = ('There are ' + ___ + ' numbers that are between the two numbers);
    document.getElementById('numberExist').innerHTML = numberExist;
}
我知道我没有提供太多,但如果我自己尝试这样做,我会发疯的

var lower=18;
var lower = 18;
var upper = 20;
var array = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20];
var between = array.filter(function(item) {
  return (item > lower && item < upper);
});
var上限=20; var数组=[18,23,20,17,21,18,22,19,18,20]; var between=array.filter(函数(项){ 返回(项目>下部和项目<上部); });
var listOfvalues=[];

对于(var i=0;i18&&array[i],为Wei的答案提供过滤解决方案

var array = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20];
var lower = 18;
var upper = 20;

var result = array.filter(function(item) {
  return item >= lower && item <= upper;
});

document.getElementById('listOfvalues').innerHTML = ('There are ' + result.length + ' numbers that are between the two numbers);
var数组=[18,23,20,17,21,18,22,19,18,20];
var较低=18;
var上限=20;
var result=array.filter(函数(项){

return item>=lower&&item一种方法如下:

// a changed (hopefully meaningful) name for the function,
// lower: Number, the lower-boundary,
// upper: Number, the upper-boundary,
// haystack: Array, the array of values:
function numbersBetween(lower, upper, haystack) {

  // if every value of the Array is, or can be coerced to,
  // a Number then we continue to work with that Array:
  if (haystack.every(value => Number(value))) {

    // Here we use Array.prototype.filter() to filter the
    // values of the Array according the function:
    return haystack.filter(

      // here we use an Arrow function, since we don't need
      // to use a 'this'; here we retain the current value ('n')
      // in the Array if that Number is greater than the
      // lower-boundary and lower than the upper-boundary:
      n => n > lower && n < upper
    );
  }

  // otherwise, if not every value is, or can be coerced,
  // to a Number we simply return an empty Array:
  return [];
}

// this caches the element with the id of 'output':
let list = document.getElementById('output'),

  // we create an <li> element:
  li = document.createElement('li'),

  // we create a document fragment:
  fragment = document.createDocumentFragment(),

  // and an empty uninitialised variable:
  clone;

// we call the numbersBetween function, passing in the relevant
// boundaries and the Array:
numbersBetween(7, 20, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15])

// because the function returns an Array (either an empty Array or
// an Array with values), we can chain the function using
// Array.prototype.forEach():
.forEach(

  // here we use another Arrow function expression:
  num => {

    // clone the created <li> element and assign it
    // the 'clone' variable:
    clone = li.cloneNode();

    // set the clone's textContent to be equal to
    // the current Array-element of the Array over
    // which we're iterating:
    clone.textContent = num;

    // and append that cloned-element to the
    // document.fragment:
    fragment.appendChild(clone);
  });

// this could be used in the last iteration of the
// Array.prototype.forEach() method, but because it
// generates no errors (an empty document fragment
// can be appended to an element without consequence),
// we perform this step here, appending the document
// fragment to the cached 'list' element which appends
// the nodes contained within the document fragment to
// the specified parent-node (list):
list.appendChild(fragment);

    您可以测试该值是否在给定范围内,并对其进行计数

    var数组=[18,23,20,17,21,18,22,19,18,20],
    下限=18,
    上限=20,
    结果=数组.reduce(函数(r,a){
    
    返回r+(a>=lower&&a请添加所需的结果。最终输出将显示这两个值之间的值数。因此,在我使用18和20的示例中,最终结果应为“6”。这将随约束条件的不同而变化。请突出显示,如何获得
    6
    。在18和20之间,有18、20、18、19、18,20。如果,假设数字是20和30,5个数字符合标准,23,20,21,22,20。希望这有助于这项工作。谢谢。一个问题可以帮助我理解你的代码。函数(项)的用法如何工作?我习惯于在代码中的其他地方调用函数,而不是以您所展示的样式。虽然这样做有效,但我不建议这样的解决方案,因为您需要两次迭代,并创建两个包含项的数组,而您只需要它的计数。使用函数(项)表示该项取代数组中的每个元素。在您的情况下,第一次使用的项是18,然后是23、20,依此类推。如果答案满足您的问题,请接受。您的复杂性目前为O(2n)所以,如果你检查一下item>lower&&item,你的性能可以提高一倍。谢谢@Jonasw,我已经修改了它。返回haystack.reduce((a,n)=>(a?)((n>lower&&nIt)也有点难读;我会牺牲速度来提高可读性(几乎)每一次。我可能只是一个业余爱好者,但如果有人在他们的产品代码中使用我的答案,我想在一定程度上确保他们不会因为自己的同事知道他们住在哪里而受到威胁……:)touchésir;)。我写的不可读,因为它在评论部分,可以很容易地扩展以提高可读性…这很好。谢谢。不过有一个问题可以帮助我理解你的代码。函数(r,a)的使用如何工作?我习惯于在代码中的其他地方调用函数,而不是以您介绍的方式。另外,我将如何以我习惯于调用函数的方式使用您的代码?
    // a changed (hopefully meaningful) name for the function,
    // lower: Number, the lower-boundary,
    // upper: Number, the upper-boundary,
    // haystack: Array, the array of values:
    function numbersBetween(lower, upper, haystack) {
    
      // if every value of the Array is, or can be coerced to,
      // a Number then we continue to work with that Array:
      if (haystack.every(value => Number(value))) {
    
        // Here we use Array.prototype.filter() to filter the
        // values of the Array according the function:
        return haystack.filter(
    
          // here we use an Arrow function, since we don't need
          // to use a 'this'; here we retain the current value ('n')
          // in the Array if that Number is greater than the
          // lower-boundary and lower than the upper-boundary:
          n => n > lower && n < upper
        );
      }
    
      // otherwise, if not every value is, or can be coerced,
      // to a Number we simply return an empty Array:
      return [];
    }
    
    // this caches the element with the id of 'output':
    let list = document.getElementById('output'),
    
      // we create an <li> element:
      li = document.createElement('li'),
    
      // we create a document fragment:
      fragment = document.createDocumentFragment(),
    
      // and an empty uninitialised variable:
      clone;
    
    // we call the numbersBetween function, passing in the relevant
    // boundaries and the Array:
    numbersBetween(7, 20, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15])
    
    // because the function returns an Array (either an empty Array or
    // an Array with values), we can chain the function using
    // Array.prototype.forEach():
    .forEach(
    
      // here we use another Arrow function expression:
      num => {
    
        // clone the created <li> element and assign it
        // the 'clone' variable:
        clone = li.cloneNode();
    
        // set the clone's textContent to be equal to
        // the current Array-element of the Array over
        // which we're iterating:
        clone.textContent = num;
    
        // and append that cloned-element to the
        // document.fragment:
        fragment.appendChild(clone);
      });
    
    // this could be used in the last iteration of the
    // Array.prototype.forEach() method, but because it
    // generates no errors (an empty document fragment
    // can be appended to an element without consequence),
    // we perform this step here, appending the document
    // fragment to the cached 'list' element which appends
    // the nodes contained within the document fragment to
    // the specified parent-node (list):
    list.appendChild(fragment);