Tell us what’s happening:
I need help to know why my code does not return the index, so far the loop arranges the elements in array in ascending order but the second loop doesn’t seem to bring the right answers in all the tests. It also doesn’t bring the right counts
Your code so far
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
let newArr=[];
let count=0;
for(var i=0; i<arr.length; i++){
let largerNum=arr[0];
if(largerNum<arr[i]){
newArr[0]=largerNum;
newArr.push(arr[i]);
console.log(newArr);
}
for (var j=0; j<arr.length; j++){
if(num>newArr[j]){
count++;
}
}
}
return count++;
}
console.log(getIndexToIns([10, 20, 30, 40, 50], 30));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36.
I used an outo-formatter to tidy up your code
when the second loop should run? when does it run?
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
let newArr = [];
let count = 0;
for (var i = 0; i < arr.length; i++) {
let largerNum = arr[0];
if (largerNum < arr[i]) {
newArr[0] = largerNum;
newArr.push(arr[i]);
console.log(newArr);
}
for (var j = 0; j < arr.length; j++) {
if (num > newArr[j]) {
count++;
}
}
}
return count++;
}
console.log(getIndexToIns([10, 20, 30, 40, 50], 30));
an hint to make things easier: you are counting how many numbers are lower than the number to put in - to count them do they need to be in a specific order?
i wanted to know whether this approach would also work, my main aim was to arrange them in order and just get the index of the position where they would fit
that not your output , that’s what the console log prints at each iteration
to see the output you need to wrap the function call in a console log
to see the final value of a variable
you need to wrap it in a console.log after the changes have finished
I suggest you add a label to your console logs to have more infos on what is being printed in the console
like console.log("array inside loop", JSON.stringify(newArr)) (the stringify is to have the array print as an array whenever you are using it, the fcc console has a weird behaviour)
first you have to sort array. sorting an array , check for sorting algorithm .
I prefer to use bubble sort algorithm. try to impediment that. if not , I think this challenge allow you to use already defined sorting method in JavaScript. one you ensure that, your array is sorted, then you have to check four condition.
second part
first condition, if array is empty, it should return 0
second condition, getIndexToIns([10, 20, 30, 40, 50], 35) should rerun index as 3
because 35 is less than 40. so you should rerun index of 40 . that is three
third condition
getIndexToIns([10, 20, 30, 40, 50], 30) should return 2
because index of 30 in array is 2
last condition getIndexToIns([2, 5, 10], 15) should return 3
because 15 is not a value in the array. so just return length of array
posting this explanation after i wrote my own code and get the challenge passed
it is clearly mention in the challenge itself to sort first argument . I did not understand why you told me that it should not be sorted. below is my working solution
yes you are right . there is no need of sorting . only need to return the number of element in first argument(array) which is lesser than the second argument