Count number of values in array between two input values

前端 未结 4 1750
鱼传尺愫
鱼传尺愫 2021-01-28 17:05

As the title suggests, I want to create a function the counts the number of values in my array between two values that have been entered by the user. So for example, if the arra

4条回答
  •  执笔经年
    2021-01-28 17:30

    You may do as follows;

    var arr = [1,4,6,7,8,6],
      input = [5,7],
     result = arr.reduce((r,n) => n >= input[0] && n <= input[1] ? ++r : r, 0);
    console.log(result);

提交回复
热议问题