Where do I Belong (Array unsorting itself randomly

Tell us what’s happening:
Ok so basicly ive setup a little loop which checks the values in an array if one is found to be smaller it is cut out and stuck at the front of the array the console.logs show it to work but it seems to unsort its self randomly then if i delet say 1 0 from a randomly selected for loop like i just delet the 0 and put it back it resorts itself can somneone explain whats going on here cause im really lost

Your code so far


function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
arr.push(num);
//console.log(arr)
for (let i = 0 ; i < arr.length ; i++){
 // console.log(i)
  for (let j = 0 ; j < arr.length ; j++){
    if (arr[i] > arr[j]){
      let temp = arr.splice(j, 1)
      //console.log(temp)
    j = 0;
      arr.unshift(temp)
   //  console.log(arr) 
    }

  }

}
for (let p = 0 ; p < arr.length ; p++){
console.log(arr)
if (num == arr[p]){
  num = p;
  console.log(p)
}
 
}
console.log(arr)
 return num;
}

getIndexToIns([60, 40, 70, 90], 50);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/where-do-i-belong

First, as debugging tool I suggest you start using console.log(JSON.stringify(...)) in the FreeCodeCamp editor, you would have seen what I talk below.

You have one big issue that makes your code an infinite loop…

You can check what’s going on here, where I have added many console.log statements to your code (you can follow exactly what happens in the console after you press run): https://repl.it/@Ieahleen/CompatibleCooperativeBrackets

Always always be sure to know what your methods do:

Return value: An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

Your code is still an infinite loop after fixing that, but the cause is changed.

1 Like

hey ya thanks alot i got there in the end think i made it abit harder for myself than i needed the json console.logs are very useful ty for the tips i finished it however im not 100% happy with my code as i have one variable which doesnt feel consistent to writing reusable decent code i have a variable randomly named cheese which i used to parse the single returned array into and after i check to see where that interget should be put into the array the only way i manged to achive this is by inside the for in loop i made setting cheese to a ridiously high number (cheese = 99999) so that it doenst just keep adding cheese on the end. little confused as to why it would anyway as im not using equal to only better than operaters in the logic. Is there a better or more used syntax maybe one which doesnt use such a ‘bad fix’
thanks again!

Your code so far


function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
arr.push(num);
let hold = 0
//console.log(arr)
for (let i = 0 ; i < arr.length ; i++){
 // console.log(i)
  console.log("arr[i]: " + JSON.stringify(arr[i]));
  for (let j = 0 ; j < arr.length ; j++){
    if (arr[i] > arr[j]){
      let temp = arr.splice(j, 1)
      console.log("arr[j]: " + JSON.stringify(arr[j]))
      let cheese = parseInt(temp)
   //  console.log(cheese)
    for (let x in arr)
      if (cheese < arr[x]){
        arr.splice(x, 0, cheese)
        console.log(arr)
        cheese = 99999;
      }
    }
  }

}
for (let p = 0 ; p < arr.length ; p++){
if (num == arr[p]){
  num = p;
}
}
console.log(arr)
console.log(num)
 return num;
}
getIndexToIns([10, 20, 30, 40, 50], 35);

Your code’s logic is a bit over complicated. If I ask you to count the number elements of arr which are less than the value of num, would you know how to arrive at that count? If so, then you would know the index at which num belongs.

You only need to iterate through arr a single time. This can be accomplished with a single for loop or while loop.

hi thanks for the reply! would that invole using the sort method discribed in the hint section to sort it seprately and then use a loop to go over and place num in the correct place?

also im still intrested to know about my above question would using break be a more effective way

There is no reason to sort the array first. As I stated before, you only need to iterate the array one time. Try to implement what I described. If you get stuck, ask for help.

ok i get it the way the question was phrased ‘once it has been sorted.’ made me think they wanted me to sort the array and i thought if im sorting the array might as well stick the num in there too but i guess they just wanted me 2 count each number which was lower than num and return the number lol a fair bit easyer

Yes, you could actually sort the array first and then iterate through the array to find the index where num would go, but that is less efficient than the method I explained. Sometimes it is just a matter of thinking about the steps (the algorithm) needed and then realizing there may be a more efficient algorithm that would cut out certain steps.