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);
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);
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.
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.
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);
}
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);
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.
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);
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);
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);
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);
}
}