0

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 array was [1, 4, 6, 7, 8, 6] and the user entered 5 as their first value and 7 as their second value, they would be greeted with an alert that said

"total number of values = 3".

2
  • OK. So where are you stuck? You forgot to ask a question. Simplest solution is probably a plain for loop containing an if statement... (Or if you want to get "fancy", you could use the array .reduce() method.) Commented Apr 9, 2017 at 3:27
  • Can array possibly contain numbers greater than 9? Commented Apr 9, 2017 at 3:58

5 Answers 5

1

All you need is a simple for loop.

var total = 0;
var num1 = 5;
var num2 = 7;
var array = [1,4,6,7,8,6];
for(var a = 0; a < array.length; a++) {
    if(array[a] >= num1 && array[a] <= num2) {
         total++;
    }
}
alert("Total numbers of values = " + total);

This will loop through the array, detect nums within the range, tally up a value, and output it in a alert.

Sign up to request clarification or add additional context in comments.

2 Comments

i just tried this, but no matter what I enter it keeps saying the total number of values is 0?
Working fine for me. jsfiddle.net/wehw24L0 What does your code look like.
1

You can create an extremely clean solution to this problem by utilizing the second property of Array#filter (which sets the this binding given to your callback of choice):

var array = [1, 4, 6, 7, 8, 6]

function inRange (x) {
  return this[0] <= x && x <= this[1]
}

var result = array.filter(inRange, [5, 7]).length

console.log('Total number of values:', result)

Comments

0

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);

5 Comments

"two values that have been entered by the user" - What if the user enters "5" and "17"?
@nnnnnn Would need to adjust approach var len = arr.filter(n => n >= from && n <= to).length;
arr.filter(/./.test, RegExp(`[${from}-${to}]`, "g")) is simpler
@dandavis Can you describe pattern? RegExp constructor is set to this within .filter(), yes? What is purpose for /./? Same as RegExp.prototype?
/./.test passes the generic .test() rx method, the 2nd argument tells that method what this is. simpler example: filter for even integers: [0,1,2,3,4].filter(/./.test, /[02468]$/) or find Capitals: [].filter.call("Dan Davis", /./.test, /[A-Z]$/)
0

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);

Comments

0

var array = [1, 4, 6, 7, 8, 6];

function countNumber(arr,a,b){
    let count = 0;
    for (const number of arr){
        if (number >= a && number <= b){
            count ++;
        }
    }
    return count;
}

console.log(countNumber(array, 5, 7));

1 Comment

you should also add an explanation to your code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.