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
You can use Array.prototype.filter()
, RegExp.prototype.test()
with RegExp
constructor with class from-to
, where from
is 5
, to
is 7
, get .length
of resulting array
var from = 5;
var to = 7;
var len = arr.filter(RegExp.prototype.test.bind(new RegExp(`[${from}-${to}]`))).length;
You can alternatively use .toString()
, .match()
var arr = [1,4,6,7,8,6];
var from = 5;
var to = 7;
var res = arr.toString().match(new RegExp(`[${from}-${to}]`, "g"));
var len = res.length;
console.log(res.length);