Where do I belong .......help plzz

Where do I belong .......help plzz
0

#1

Tell us what’s happening:

Your code so far

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

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
 a= arr.sort(function(a,b){
   return a-b;
 });
  console.log(arr);
  var result;
  for(var i=0;i<arr.length;i++){
    if(a[i]>=num){
      result=i;
      break;
    }
    else if(i==(arr.length-1)&& a[i]<=num){
      result=i;
    }
    
  }
 
  return result;
}

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

 

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:

what is wrong with this code the above input is not passing the test…!!


#2

You have two issues:

#1) You only need one of the following line. You have two (showing in the code you posted above).

function getIndexToIns(arr, num) {

#2) The reason your code does not pass the one test of getIndexToIns([2, 5, 10], 15) , is best explained by looking at the evaluation of the if and if else statements during the for loop iterations.

1st Iteration - i = 0, so the if (a[0] >= num) evaluates to false because 2 >= 15 is false. The else if (i==(arr.length-1)&& a[i]<=num) evaluates to false because 0 == 2 is false. All it takes is one false when using &&, so the entire statement is false.

2nd iteration - i = 1, so the if (a[1] >= num) evaluates to false because 5 >= 15 is false. The else if (i==(arr.length-1)&& a[i]<=num) evaluates to false because 1 == 2 is false. All it takes is one false when using &&, so the entire statement is false.

3rd iteration - i = 2, so the if (a[2] >= num) evaluates to true because 15 >= 15 is true. Then you assign i (which is 2) to result and break out of the for loop.

Your function returns 2, because result is 2.


#3

There are 2 problems that I see ,

Number 1 is with the sorting function that you are using, note the following from MDN

The sort() method sorts the elements of an array in place and returns the array. The sort is not necessarily stable. The default sort order is according to string Unicode code points.

That is why It’s better to go with the callback function of sort() method always and specify how you want your sorting to be done, for example to sort ascending you could use

arr.sort((a,b)=>{
 return a-b;
})

read more about sorting with the callback function here:

The second problem that I see is with your comparison function and your if statement , you maybe overthinking it, if the number you are checking is less than or equal to the array element that it is being checked against, that is sufficient to break the loop and return the index. You don’t really need anything ‘else’ (pun intended)

hope that clarifies if not let me know…

oops just saw @RandellDawson response too


#4

The OP is using:

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

The assignment to a does add any value, because (like you said) the sort of arr is in place.

UPDATE: @Dereje1 - It appears the OP had already edited their original post from using plain sort to using the callback function method as you suggested, so just ignore my comment.


#5

Ah. I see he wasn’t at first, I copied the code was doing something else and continued writing, this was the original code I copied ,


#6

Yeah, that is what I figured. See my updated post above.