Bonfire Algorithm Challenge: Where Do I Belong on javascript

Hello guys I am currently stuck on this algorithm challenge on FCC. This is what the challenge is all about:

Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.

For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).

Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).

This is my code here:

function getIndexToIns(arr, num) {
var getIndx;

var newArr = arr.sort(function (a, b) {
return (a - b);
});

for (i = 0; i < newArr.length; i++) {
if (num > newArr[i]) {
getIndx = newArr.indexOf(newArr[i]);
}
}

return getIndx;
}

getIndexToIns([10, 20, 30, 40, 50], 35);

when I ran the code it worked but it isn’t passing the test. Guys I need your help. Thanks

Stack Overflow Asked by Johnbosco on January 28, 2021

You could iterate and check if the actual value is greater then the number. Then retuen the actual index. If no value match, then return the length of the array as new input index.

function getIndexToIns(arr, num) {
var i;
arr.sort(function(a, b){
return a - b;
});
for (i = 0; i < arr.length; i++) {
if (arr[i] > num) {
return i;
}
}
return arr.length;
}

console.log(getIndexToIns([10, 20, 30, 40, 50], 35)); // 3
console.log(getIndexToIns([1, 2, 3, 4], 1.5));        // 1
console.log(getIndexToIns([20, 3, 5], 19));           // 2
console.log(getIndexToIns([4, 3, 2, 1], 5));          // 4

Alternative version without using a method from the Array API

function getIndexToIns(array, value) {
var i = array.length,
p = 0;

while (i--) {
p += array[i] <= value;
}
return p;
}

console.log(getIndexToIns([10, 20, 30, 40, 50], 35)); // 3
console.log(getIndexToIns([1, 2, 3, 4], 1.5));        // 1
console.log(getIndexToIns([20, 3, 5], 19));           // 2
console.log(getIndexToIns([4, 3, 2, 1], 5));          // 4

Correct answer by Nina Scholz on January 28, 2021

My solution to this problem.

const getIndexToIns = (arr, num) => {
let newArr = arr.concat(num);

newArr.sort(function(a, b) {
return a - b;
});

let index = newArr.indexOf(num);

return index;

}

Answered by Atudosiea Catalin on January 28, 2021

Thanks Guys for all your assistance. I figured it out and this is the final code here

function getIndexToIns(arr, num) {

var getIndx;

var newArr = arr.sort(function(a, b){
return (a - b);
});

for (i = 0; i < newArr.length; i++) {

if (newArr[i] > num || newArr[i] === num) {

return i;
}

}

return newArr.length;
}

Answered by Johnbosco on January 28, 2021

The solutions proposed so far tend to follow literally the request of the problem: first sort the array, then find the index where to insert the number. That brings you to loop over the array twice. More if you have to clone the array. That is slow, even without considering all the garbage we end up creating ...

Can we do better? I think so.

How about "count how many numbers in the array are less or equal to the number to insert". This achieves the same goal but let's us do it looping only once over the array.

function getIndexToIns(arr, num) {
return arr.reduce(function (c, x) { return x <= num ? c+1 : c }, 0);
}

console.log(getIndexToIns([10, 20, 30, 40, 50], 35)); // 3
console.log(getIndexToIns([20,3,5], 19)); //2
console.log(getIndexToIns([1,2,3,4], 1.5)); //1
console.log(getIndexToIns([1,2,3,4], 1)); //1
console.log(getIndexToIns([1,2,3,4], 0)); //0

how about that?! twice as fast*, yay!

* it's probably not really twice as fast, if you have to be nitpicky about my claims...

EDIT

Actually I can do better. That ternary if introduces some branching in the code that bothers me. We could take advantage of javascript weirdness to avoid it

arr.reduce(function (c, x) { return c + (x <= num) }, 0)

why? because when combined with a numeric operation true is converted to 1 and false to 0
That removes the extra branching from the code so it's going to be slightly faster than the previous version ... And it would also be easier to unit test, if you care about that.

Answered by alebianco on January 28, 2021

If I understand correctly I think you should look at the previous item from the list:

function getIndexToIns(arr, num) {
var getIndx = 0;

var newArr = arr.sort(function (a, b) { return (a - b); });
for (i = 1; i < newArr.length; i++) {
if (num >= newArr[i-1]) {
getIndx = i;
}
}

return getIndx;
}

console.log(getIndexToIns([10, 20, 30, 40, 50], 35)); // 3
console.log(getIndexToIns([20,3,5], 19)); //2
console.log(getIndexToIns([1,2,3,4], 1.5)); //1
console.log(getIndexToIns([1,2,3,4], 1)); //1
console.log(getIndexToIns([1,2,3,4], 0)); //0

Answered by Mark Knol on January 28, 2021

Related Questions

Flight destination check programm – C++

3  Asked on January 12, 2021 by mrfreeman16

Jquery datatables inbuilt sort is not working

1  Asked on January 12, 2021 by karthik

How can implement EM-GMM in python?

2  Asked on January 12, 2021

How to execute (./myscript) inside awk or bash script?

3  Asked on January 12, 2021 by redouane-nouh

How can I remove the space between paragraphs using python-docx?

0  Asked on January 12, 2021 by mario-tunon

Disable nuxt link based on boolean

3  Asked on January 12, 2021 by samantha

Error Using Drive Mount with Google Colab

1  Asked on January 12, 2021 by nachengue

How to extract slope of all the peak containing curves in a graph?

1  Asked on January 12, 2021 by vivz

Android Preventing Double Click On A Layout with handher

1  Asked on January 12, 2021 by user13380366

How to center align the label text inside the option tag?

1  Asked on January 11, 2021 by mr-spock

How can I read the CSS for mobile images

0  Asked on January 11, 2021 by power-rico

Deleting from array using unset creates values

2  Asked on January 11, 2021 by cameron

Output should look like a quotation

4  Asked on January 11, 2021 by maurya

Sorting DateTime column in ASP.NET MVC

2  Asked on January 11, 2021 by steven85791

Rounding does not match all expected results

1  Asked on January 11, 2021 by aa-44

A package that I am using is not working in Javascript/HTML

1  Asked on January 11, 2021 by york910

How to create mutually referencing data structures in Haskell?

3  Asked on January 11, 2021 by alejandro-navas

Call to make pair with mutex as argument fails. Cannot insert mutex onto unordered map

1  Asked on January 11, 2021 by brian-yeh