Where do i belong -js chapter

Tell us what’s happening:

Your code so far


function getIndexToIns(arr, num) {

arr.push(num)
for (let i =0; i<arr.length ; i++) {
  if (arr[i] ===num) {
    delete arr[i];
  }else {return arr.indexOf(num)}
}
}

console.log(getIndexToIns([40, 60], 40));

Your browser information:

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

Challenge: Where do I Belong

Link to the challenge:

arr is [40, 60]
num is 40

push 40 to arr
arr is [40, 60, 40]

run a loop (length of arr is 3):

  1. arr[0] is 40, that equals 40, delete for some reason
    arr is now [null, 60, 40]
  2. arr[1] is 60, that is not 40, return the first index found for the value 40
    arr is [null, 60, 40], index of 40 is 2, so return 2 and finish