1

It's example of array which contains only numbers:

var array = [3.1, 1, 2.2, 5.1, 6, 7.3, 2.1, 9]

EDITED

How to count how many(edited from word 'sum') of elements are in array below / above given number eg. '5.25'?

So answer should be 5 elements are below (3.1, 1, 2.2, 5.1, 2.1) and 3 element are above (6, 7.3, 9)

6
  • why not use third party like lodash....will make things easy Commented Nov 1, 2017 at 8:53
  • 2
    @RohitasBehera "Easy" is not the goal, learning is. Commented Nov 1, 2017 at 8:54
  • @RohitasBehera 1. I can't use lodash in that case 2. It's question about pure js Commented Nov 1, 2017 at 9:03
  • agreed...my answer is just good to know ...might be be useful in some oder scenario Commented Nov 1, 2017 at 9:04
  • You changed the total question. Commented Nov 1, 2017 at 9:10

4 Answers 4

4

Use Array#reduce with an object { above: 0, below: 0 } as initial value. On each iteration check each number against the media, and accordingly add 1 to above/below.

Note: This is an array of strings. I've converted them manually to numbers. I also assume that a number that equals the median should be added to below. If you want to skip this numbers, change the comparison to <.

var array = [3.1, 1, 2.2, 5.1, 6, 7.3, 2.1, 9];

var median = 5.25;

var counts = array.reduce(function(s, n) {
  s[n <= median ? 'below' : 'above'] += 1;
  
  return s;
}, { above: 0, below: 0 });

console.log(counts);

If you must have an array of strings, you can convert to a number in run time using String#parseFloat or the + operator:

var array = ['3.1', '1', '2.2', '5.1', '6', '7.3', '2.1', '9'];

var median = 5.25;

var counts = array.reduce(function(s, str) {
  s[+str <= median ? 'below' : 'above'] += 1;
  
  return s;
}, { above: 0, below: 0 });

console.log(counts);

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

5 Comments

how you converted a string array to number array ??
@RohitasBehera strings was no main case here
@OriDrori thanks for answer but... sorry I asked question in not correct way :| I edited question
@CodeJoe - the answer was already updated to fit the question. Try again.
@OriDrori great! Thanks a lot :)
3

No need for reduce, because the result does not change by using reduce, it is still an object.

var array = [3.1, 1, 2.2, 5.1, 6, 7.3, 2.1, 9],
    value = 5.25,
    counts = { below: 0, equal: 0, above: 0 };

array.forEach(function(v) {
    counts[{ '-1': 'below', 0: 'equal', 1: 'above' }[Math.sign(v - value)]]++;
});

console.log(counts);

Comments

0

Assuming you are using an array of numbers..

I use lodash ...it helps shorten the code &

var array = [3.1, 1, 2.2, 5.1, 6, 7.3, 2.1, 9];


console.log(_.sum(array));   //  _.sum  does the addition
<script src="https://cdn.jsdelivr.net/lodash/4.14.1/lodash.min.js"></script>

Comments

0

There are couple of strategies but I will use the one thats easy to read and understand:

(1) Create a new array with all elements that fit the criteria aka <=5.25

let sampleArr = ['3.1', '1', '2.2', '5.1', '6', '7.3', '2.1', '9'];

let sortedArr = sampleArr.filter(function(e){
     return parseInt(e) <= 5.25
});

Notice I wrote parseInt(e), that converts the string into a number. The values you gave in your array were all strings.

(2) Now simply get the length of the sortedArr like so:

let finalCount = sortedArr.length;

Hope this helps!

2 Comments

I asked question in wrong way (it's edited now). I mean "how many of elements are below/above given number". But you answer helped me :) I just need to use .filter and count length of sortedArr. Thanks
Always happy to help! Could you please select my response as the answer to the question. Thanks :)

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.