# freeCodeCamp Algorithm Challenge Guide: Where do I Belong

freeCodeCamp Algorithm Challenge Guide: Where do I Belong
0
#22

function getIndexToIns(arr, num) {

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

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

``````if(num <= newArr[i]) {

return newArr.indexOf(newArr[i]);

}
``````

}

``````  return newArr.length;
``````

}

getIndexToIns([2, 5, 10], 15);

0 Likes

#23

here is my solutiionā¦ not that good i just startedā¦ thanks

0 Likes

#24

Here is my solution.

``````function getIndexToIns(arr, num) {

function numbers(a,b){
return a-b;
}

var arrSort = arr.sort(numbers);
var arrNum = arr.push(num); arr.sort(numbers);

return arr.indexOf(num);
}

getIndexToIns([70, 60], 50);``````
0 Likes

#25

My code:

``````function getIndexToIns(arr, num) {

var index = 0;

//sort the array
arr.sort(function(a,b){
return a-b;
});

//increment index by one if num is greater than the array element
for(var i=0; i< arr.length; i++){
if(num > arr[i]){
index ++;
}
}

return index;
}

//test
getIndexToIns([3, 10, 5], 3);``````
0 Likes

#26
``````function getIndexToIns(arr, num) {
// Find my place in this sorted array.
var lol=0;
for(var i =0 ; i<arr.length; i++){
if(arr[i]<num){ lol++; }
}
return lol;
}

getIndexToIns([40, 60], 50);``````
0 Likes

#27

Guys, I understand the solutions are given here but I would like some help figuring what where my code is going wrong. It seems right to me and I have gone over it multiple times. Thanks.

The code is:

function getIndexToIns(arr, num) {
arr.push(num);

for(var q=0;q<arr.length-1;q++)
for(var j=0;j<arr.length-1-q;j++)
if(arr[j]>arr[j+1])
{
t=arr[q];
arr[q]=arr[j];
arr[j]=t;
}
for(var i=0;i<arr.length;i++)
if(arr[i]==num)
return i;

}

getIndexToIns([3, 10, 5], 3);

0 Likes

#28

I also used a very similar solution to yours at first. I inserted num, then sorted the array, then returned the indexof where num was. However I then re-read the instructions and I felt like this method was sort of cheating. The instructions state:
ā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.ā

To me, the instructions want you to find out where the number should be inserted, WITHOUT inserting the number.

I went back and had to spend quite a bit more time finding a solution that worked this way. I ended up with one similar to the first Beginner solution except I added an If statement that wasnāt needed. I love the way that code ends with āReturn arr.lengthā. I made that much more complicated with an If/Else statement that I didnāt need.

0 Likes

#29

function getIndexToIns(arr, num) {
// Find my place in this sorted array.

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

var control = 0;

for(var i=0;i<newArr.length;i++){
if(newArr[i]<num){
control = i+1;
}
}

return control;

}

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

0 Likes

#30
``````function getIndexToIns(arr, num) {
sorted = arr.sort(function(a, b) {
return a - b;
});
let highestNum = arr.filter(x => x >= num);
if(highestNum.length === 0) {
highestNum.push(num);
return arr.length;
} else {
let index = arr.indexOf(highestNum[0])
return index;
}

}
getIndexToIns([2, 5, 10], 20)
``````

Here is another solution ā Iāve got inspired by the solution at the top

``````function getIndexToIns(arr, num) {
return arr.concat(num)
.sort((a, b) => (a - b))
.indexOf(num);
}

getIndexToIns([2, 5, 10], 20)``````
0 Likes

#31

My answer, given below:

``````function getIndexToIns(arr, num) {
// Find my place in this sorted array.
//add num to the given array
arr.push(num);

//sort the new array
arr.sort(function (a, b) {
return a - b;
});

//return index value of the added num in our array
return arr.indexOf(num);
}
``````

`getIndexToIns([40, 60], 50);`

0 Likes

#32

Ran it like this:

``````function getIndexToIns(arr, num) {
//Sort array first
arr = arr.sort((a,b)=> a-b);

//Counter for position
var count = 0;

//Determine index
for (let i =0; i<arr.length; i++){
if (num<=arr[i]){
break;
}
count++;
}

return count;
}

getIndexToIns([5, 3, 20, 3], 5);
``````
0 Likes

#33

My solution wasnāt accepted did I have a mistake somewhere?

``````/*var count = 0;
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
for(var i = 0; i < arr.length;i++){
var temp = arr[i];
for(var j = arr.length; j > 0;j-- ){
if(temp > arr[j]){
temp = arr[j];
}
}
}
for(var z = 0; z < arr.length;z++){
if(num > arr[z]){
count++;
}
}
return count;
},``````
0 Likes

#34

I interpreted the instructions the same at first, splicing in num. My solution works, but there was something odd in the code that would slow the test down during the splice. Code follows:

``````function getIndexToIns(arr, num) {
//Sort arr first
arr.sort(function(a, b) {
return a - b;
});
// Create array of arguments
var args = []; // Empty array, at first.
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]); }
// Iterate through elements to evaluate statement that follows
for (var j = 0; j < arr.length; j++) {
for (var k = 0; k < args.length; k++) {
if (arr[j] > args[k]) {

arr.splice(j, args[k], num); // using 0 instead of args[k] slows down the test by a large margin?!

}  else {
if (arr[j] < args[k]) {
arr.push(num);
}
}
}
}

return arr.indexOf(num);
}

getIndexToIns([2, 5, 10], 15);``````

This used everything Iāve learned from previous challenges. I would like to know why using a value of 0 instead of args[k] during my splice slowed down the test. I thought Iād crashed the browser at first, but I get immediate test reults when I use args[k] instead of 0 for the splice.

0 Likes

#35

I used filter() since Idonāt like loops that much

``````    function getIndexToIns(arr, num) {
arr = arr.sort().filter(function(val){
return val < num;
});
return arr.length;
}``````
2 Likes

#36

I belong here:

function getIndexToIns(arr, num) {
// Find my place in this sorted array.

var newArr = num;

var c = num;

var d = num;

arr.push(num);
arr.sort(function(c,d) {

``````return c - d;
``````

});
return arr.indexOf(num);
}

getIndexToIns([40, 60], 50);

0 Likes

#37

Hereās mine.

``````function getIndexToIns(arr, num) {
//Push the num argument into the array.
arr.push(num);
//Then return the sorted array followed by the indexOf method, checking for num's index.
return arr.sort(function(a, b) { return a-b; }).indexOf(num);
}

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

#38

Here is my code:

function getIndexToIns(arr, num) {
// Find my place in this sorted array.
//var srt = arr.sort();
for (i=0;i<arr.length;i++){
for(j=0;j<num;j++){
if (arr.includes(num)){
res = arr.sort();
return arr.sort(function( a , b ){ return a-b;}).indexOf(num);
}
res1 = arr.push(num);

``````  return arr.sort(function( a , b ){ return a-b;}).indexOf(num);
}
//return arr.indexOf(num);
``````

}

``````//return arr.sort()
``````

}
getIndexToIns([2, 5, 10], 15);

0 Likes

#39

My code. Very similar:

function getIndexToIns(arr, num) {
var newArr = arr.concat([num]);
newArr.sort(function(a, b) {
return a - b;
});
return newArr.indexOf(num);
}

getIndexToIns([2, 5, 10], 15);

0 Likes

#40

This is how I did it. Iām pround, because for once, I did need help to solve the problem. All I did was to consult the MDN documentation.

``````function getIndexToIns(arr, num) {

// creating new array to contain both arr and num

var newArray = [];

// for-loop to push each iteration of arr into newArray

for (var i = 0; i < arr.length; i++){
newArray.push(arr[i]);
}

// pushing num into newArray

newArray.push(num);

// sorting newArray

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

// returning the index of num from newArray

return newArray.indexOf(num);
}

getIndexToIns([5, 3, 20, 3], 50);``````

Returns 4 in console.

1 Like

#41

Iād like to contribute my reply to the beginner category as well, as late as it may be. For some reason, I really hate using for loops, so I created two sorting functions. I then use an if-else statement that checks whether the number to insert is larger than the largest in the array. If the check fails, it then kicks to an else statement that uses a ternary operator to determine whether or not `num` is in `arr`. If so, it will find the index of num. If not, it will find the index of a number that is greater than `num`.

``````  // Find my place in this sorted array.
function up(curr, prev) {
return curr - prev;
}
function greater(number) {
return number > num;
}
if (num > arr.sort(up).slice(arr.length-1)) {
return arr.sort(up).push(num) - 1;
} else {
return arr.includes(num) ? arr.sort(up).indexOf(num) : arr.sort(up).findIndex(greater);
}
}
``````
0 Likes