If anyone could help me figure out what is wrong with my code. It works for some of the tests but not the others. Thanks!
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr = arr.sort(function compare(a, b){return a - b;});
index = 0;
for(var x in arr){
if(x <=num){
index = x - 1;
}
}
return index;
}
getIndexToIns([10, 20, 30, 40, 50], 35);
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr = arr.sort(function compare(a, b){return a - b;});
for(var x in arr){
if(arr[x] <= num){
index = x - 1;
console.log(index);
}
}
}
getIndexToIns([10, 20, 30, 40, 50], 35);
This is where Iām at now and itās still only passing some of the tests.
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr = arr.sort(function compare(a, b){return a - b;});
index = 0;
for(var x in arr){
if(arr[x] >= num){
index = x - 1;
}
}
return index;
}
getIndexToIns([5, 3, 20, 3], 5);
No, in fact you use āx - 1ā because your condition works twice as the array contains two elements which are >= num. Thatās wrong way, think how you can avoid it.
Sorry for the delayā¦ if you are still have ???'s try reading my first comment again, and see if your code follows those steps. For more detail, check out my example below :
//Before
arr = [4, 6, 5, 8, 7,10];
num = [9];
//Notice- arr is unsorted, and does Not include ā9ā
// After
arr = [5,6,7,8,9,10];
num = [9];
//even though we add num to arr, num is still equal [9];
//You need to concat, so num will now show in arr, then sort, then find the indexOf 9 within arr.
*** Summary:** How do you make this problem one big array containing arr and num? How would you sort the array? How do you find the ānumā within the array?
Thank you for all of your help, I know this code works because when I run the test it returns the correct value, although it says its not passing any of the tests even though itās returning the correct value. What should I do?
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr.push(num);
arr = arr.sort(function compare(a, b){return a - b;});
for (var x in arr){
if(arr[x] == num){
return x;
}
}
}
getIndexToIns([10, 20, 30, 40, 50], 30);
just finished this one without any problems. However, I much appreciate your thoughts on elegance and efficiency.
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr.push(num);
arr.sort(function(a,b){
return a -b;
});
return arr.indexOf(num);
}
getIndexToIns([40, 60], 50);
That was almost my same answerā¦ I just used concat instead of pushā¦ But same result. Once you get through the advanced Bonfires, and take a look back at theseā¦ you wonāt believe how much you have learned!
And if you want to see the ugliest code ever that passes the test then here ya go.
function getIndexToIns(arr, num) {
function compareNumbers(a, b) {
return a - b;
}
// Find my place in this sorted array.
var sortedArray = arr.sort(compareNumbers);
var position;
console.log(sortedArray);
if(num<=sortedArray[0]){
position=0;
}
else if(num>sortedArray[sortedArray.length-1]){
position = sortedArray.length;
}
var i=0;
while(i<sortedArray.length){
if(num>sortedArray[i] && num<=sortedArray[i+1]){
position = i+1;
}
i++;
}
console.log(position);
return position;
}
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr.push(num);
arr.sort(function(a,b){return a-b;});
return arr.indexOf(num);
}
This is why Iāve had so much fun learning this stuff. I try and try until I finally come up with a solution that worksā¦ then I come on here and see the solutions others have posted that approach the problem in a completely different way to get the same result. Iām picking up on things, but these forums are definitely helping me find where I could clean up my code and/or shorten things up. What I came up with is a lot longer than some of the other solutions, but works:
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr.sort(function(a,b){
return a - b;
});
var last = arr[arr.length-1];
var first = arr[0];
if (num <= first) {
return 0;
}
if (num > last){
return arr.length;
}
for (i=0; i<arr.length; i++){
var lower = arr[i];
var upper = arr[i+1];
if (num >= lower && num <= upper){
return i+1;
}}}
Thanks for the hint, I rewrote my solution, using indexOf instead of a for loop makes the solution a lot cleaner. I made one change though, instead of using concat, I pushed the number into the array.