1

I have an array of integers. How can a determine a value's index depending on array, if I was to insert it into the array, so that no value before it is bigger.

Example:

let value = 3941;

calcOrder(value);

calcOrder(value) {
    let array = [3002,2934, 1044, 2848, 1293, 9342, etc..]
        // some logic
    }
}

Result: According to this example, my function should return 5 ( cause 3941 < 9342 but more than other values in array ).

2
  • 1
    That's a simple for loop with some basic math operations... Commented Sep 15, 2018 at 5:21
  • This question is underspecified because it doesn't say whether the desired index should be the index of a sorted array or based on the current order. It's convenient that the answer should be 5 in the example, but what if the array is: [10000, 3002,2934, 1044, 2848, 1293, 9342, 1]? Commented Sep 15, 2018 at 5:50

6 Answers 6

4

You can use .filter() method of arrays:

let value = 3941,
    array = [3002,2934, 1044, 2848, 1293, 9342];
    
let result = array.filter(v => v < value).length;

console.log(result);

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

Comments

2

You can write a loop to check the logic and count the numbers using length

DEMO

function countOthers(value) {
  return function(element, index, array) {
    return (element < value);
  }
}
var filtered = [3002,2934, 1044, 2848, 1293, 9342].filter(countOthers(3941)).length;
console.log(filtered);

Comments

1

You can use filter() to get the array of all the elements which are less than the value. Finally take the length of the array:

let value = 3941;

console.log(calcOrder(value));

function calcOrder(value) {
    let array = [3002,2934, 1044, 2848, 1293, 9342]
    return array.filter(i => i<value).length;
}

Comments

1

You can use Array.forEach() to loop over the array and increase the count value if the number is less than the value:

let value = 3941;
function calcOrder(value) {
  let array = [3002, 2934, 1044, 2848, 1293, 9342]
  let count = 0;
  array.forEach((item) => {
    if (item < value) {
      count++;
    }
  });
  return count;
}

console.log(calcOrder(value));

You can use old way to get that result without using array functions to make it work in IE browsers and older versions of web browsers:

let value = 3941;
function calcOrder(value) {
  let array = [3002, 2934, 1044, 2848, 1293, 9342]
  let count = 0;
  array.forEach(function(item){
    if (item < value) {
      count++;
    }
  });
  return count;
}

console.log(calcOrder(value));

Comments

1

For an O(n) solution that doesn't require creating an intermediate array, use reduce: start at 0, add one to the accumulator when the item you're iterating over is smaller than the value whose position you're looking for:

const array = [3002,2934, 1044, 2848, 1293, 9342];

const calcOrder = value => array.reduce((a, num) => num < value ? a + 1 : a, 0);
console.log(calcOrder(3941));

Comments

0

It could be a "sort" and "findIndex" logic.

var array = [3002,2934, 1044, 2848, 1293, 9342]


var calOrder = (num) => ((index = array.sort().findIndex(val => val > num)), index == -1 ? array.length : index)

calOrder(3941) // 5

Comments

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.