function getIndexToIns(arr, num) {
arr.sort(function(a, b) {
return a - b;
});
for (var i = 0; i < arr.length; i++) { // cycles through the array
if (arr[i] >= num) { // if array value is bigger than num
return i; // return index pos of num bigger than value
}
else if (arr[i] === undefined) { // if not found
arr.push(num); // push to array
return arr.indexOf(num); // return index pos of new num <-- should return 3 in this case
}
}
}
console.log(getIndexToIns([2, 5, 10], 15)); // Should return 3
The mission of this is to sort an array, and return the index value of arg2
if it were in the array.
Example: getIndexToIns([10, 20, 30, 40, 50], 35)
should return 3
.
What I’m having trouble with, is if arg2
is not found in the array, to push it into it and return its index value. I can’t seem to make it work.
We can get the indices of the sorted elements of a given array with the help of argsort() method.
function getIndexToIns(arr, num) { // Sort arr from least to greatest. let sortedArray = arr. sort((a, b) => a - b) // [40, 60]. sort((a, b) => a - b) // [40, 60] // Compare num to each number in sortedArray // and find the index where num is less than or equal to // a number in sortedArray.
argsort([1, 2, 3, 100, 5]) yields array([0, 1, 2, 4, 3]) , which appears to be the indices the OP wants. @0 0 your example is a specific case. If we run arr = [1,2,3,100, 5, 9] res = np. argsort(arr) print(res) then we get [0 1 2 4 5 3] which is wrong.
In this method, the index number is equal to the sum of prices for the year for which index number is to be found divided by the sum of actual prices for the base year.
Another way to do it:
function getIndex(arr, num) {
return arr.concat(num).sort(function(a, b) {
return a - b;
}).indexOf(num);
}
Sure there a few ways to do this but the fix in your code is below:
Working Example
function getIndexToIns(arr, num) {
arr.sort(function(a,b) {
return a-b;
});
for (var i=0;i<arr.length;i++) { // cycles through the array
if (arr[i] >= num) { // if array value is bigger than num
return i; // return index pos of num bigger than value
}
if (i === arr.length - 1) { // if not found
arr.push(num); // push to array
return arr.indexOf(num); // return index pos of new num <-- should return 3 in this case
}
}
}
in your code you checked to see if (arr[i] === undefined)
that will never happen, so instead check to see if you are at the end of the array, and if so, that means you haven't found your number, and then you can push it and get the index.
Why don't you use the .push
and .indexOf
methods on the array?
function arrSort(a, b) {
return a - b;
}
function getIndexToIns(arr, num) {
// you sort the array
arr.sort(arrSort);
// if it doesn't contain the num
if(arr.indexOf(num) == -1) {
// add the num to the array
arr.push(num);
// sort the array again
arr.sort(arrSort);
// return the index of the num
return arr.indexOf(num);
}
// if the num is in the array, return its position
return arr.indexOf(num);
}
Since your arrays seem already sorted, you should just use a dichotomic search to find the index, and then insert it with splice
.
function getIndexToIns(arr, num) {
var index = (function search(from, to) {
if(from == to) return to;
var m = Math.floor((from+to)/2);
if(arr[m] > num) return search(from, m);
if(arr[m] < num) return search(m+1, to);
return m;
})(0, arr.length);
arr.splice(index, 0, num);
return index;
}
Or, since it will be linear anyways, loop backwards manually:
function getIndexToIns(arr, num) {
for(var i=arr.length; i>0 && arr[i-1]>num; --i) arr[i] = arr[i-1];
arr[i] = num;
return i;
}
You could just push num
into the array then sort it out with map
or sort
.
function getIndexToIns(arr, num) {
arr.push(num);
arr.map(function(a,b) {
a-b;
arr.indexOf(num);
});
console.log(arr+'\n');
console.log(num +' is at '+arr.indexOf(num)+'\n');
}
getIndexToIns([2, 5, 10], 15);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With