# Basic Algorithm Scripting - Where do I Belong

Tell us what’s happening:
why does o=2 when there are only 2elements in the array

my code so far

``````function getIndexToIns(arr, num) {
let r=[...arr];
let p=[];
let o;

for(let i=0;i<arr.length;i++){
console.log(r)
o=arr.indexOf(Math.max(...r));
console.log(o)
p.push(r[o]);
r.splice(o,1)
console.log(r)

};

arr=p.reverse();
console.log(arr)
//
if(num>arr[arr.length-1]){
return arr.length;
};

if(arr.length==0){
return 0;
};
if(num==arr[0]){
return 0;
}
for(let i=0;i<arr.length;i++){
if(num==arr[i]){
return i;
}else if(num<arr[i+1]&&num>arr[i]){
return i+1;
}
}

}

getIndexToIns([3, 10, 5], 3);
``````

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

Challenge: Basic Algorithm Scripting - Where do I Belong

Because you are using the original `arr` for this, and `arr` is not changing, so when you reduce the arr `r` to `[3, 5]` then the max is `5` and that is the third element in `arr` which would be index `2`.
Also, I would highly recommend you use better variable names that `r`, `p`, and `o`. It makes it a lot harder to follow your logic using these generic names. And if you take my advice and sort the array first then you shouldn’t need any of these extra variables.