hint [1/8]
Make sure you've tried to hard to solve it yourself before proceeding. :construction:
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [2/8]
This can be a tricky problem to understand. You need to find where in the array a number should be inserted by order, and return the index where it should go.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [3/8]
The first thing to do is sort the array from lower to bigger, just to make the code easier. This is where sort comes in, it needs a callback function so you have to create it.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [4/8]
Once the array is sorted, then just check for the first number that is bigger and return the index.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [5/8]
If there is no index for that number then you will have to deal with that case too.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [6/8]
Solution ahead!
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [1/8]
Make sure you've tried to hard to solve it yourself before proceeding. :construction:
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [2/8]
This can be a tricky problem to understand. You need to find where in the array a number should be inserted by order, and return the index where it should go.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [3/8]
The first thing to do is sort the array from lower to bigger, just to make the code easier. This is where sort comes in, it needs a callback function so you have to create it.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [4/8]
Once the array is sorted, then just check for the first number that is bigger and return the index.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [5/8]
If there is no index for that number then you will have to deal with that case too.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [6/8]
Solution ahead!
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [7/8]
function where(arr, num) {
arr.sort(function(a, b) {
return a - b;
});
for (var a = 0; a < arr.length; a++) {
if (arr[a] >= num)
return parseInt(a);
}
return arr.length;
}
.sort(callbackFuntion)
to sort it by lowest to highest, from left to right.type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [8/8]
function where(arr, num) {
arr.push(num);
arr.sort(function(a, b)
{return a-b;}
);
return arr.indexOf(num);
}
Array.push()
method.Array.indexOf()
method.If you found this page useful, you can give thanks by copying and pasting this on the main chat: Thanks @Rafase282 and @rahul1992 for your help with Bonfire: Where do I belong
NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [1/1]
Make sure you've tried to hard to solve it yourself before proceeding. :construction:
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
function where(arr, num) {
// Find my place in this sorted array.
return num;
}
where([40, 60], 50);
Return the lowest index at which a value (second argument) should be inserted into a sorted array (first argument).
more info:
bf details
|bf links
|hint
hint [1/1]
Make sure you've tried to hard to solve it yourself before proceeding. :construction:
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
@Yulin777
hey fellas can you help me find why my code work partially for the bonfire "Where do I belong"
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
var array = arr.sort();
for (var i=0 ; i<array.length ; i++){
if ((num >= array[i]) && (num <= array[i+1]))
{
if (num == array[i])
return i;
return i+1;
}
}
}
getIndexToIns([40, 60], 50);
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).
Remember to use Read-Search-Ask if you get stuck. Write your own code.
Here are some helpful links:
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
var array = arr.sort();
for(var i = 0; i < arr.length; i++) {
if(num > array[i] && num < array[i + 1]) {
return array.indexOf(i + 1) + 1;
} else if (num == array[i]) {
return array.indexOf(i) + 1;
}
}
}
getIndexToIns([40, 60], 50);
help needed here.
hint [1/1]
Make sure you've tried to hard to solve it yourself before proceeding. :construction:
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]