# Basic Algorithm Scripting - Where do I Belong

why does it keep coming back undefined… am i messing up syntactically somewhere?

``````function getIndexToIns(arr, num) {
arr = arr.sort()
let vetNum;

for(let i = 0; i < arr.length; i+= 1 ){
vetNum = arr[i];

if(num > vetNum && num < vetNum + 1 ){
return arr.indexof(vetNum)
}
}

}

console.log(getIndexToIns([10, 20, 30, 40, 50], 35));

``````

User Agent is: `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36`

Challenge: Basic Algorithm Scripting - Where do I Belong

The `sort` method mutates the array in-place, so you can simply write:

``````arr.sort()
``````

This logic is the problem. For the tests cases tested, this `if` statement condition never evaluates to `true`, so after the `for` loop completes, your function returns `undefined` since the end of the function was reached before an explicit return was executed within the function.

cool cool thanks… but why is my code itself returning undefined instead of

``````arr.indexof(vetNum)
``````

why does the if statement get ignored

It is not ignored, it just never evaluates to `true`. Add the following `console.log` statement above the `if` statement and run the tests, so you can see what is being compared within the `if` statement.

``````console.log(`num = \${num}, vetNum = \${vetNum}, vetNum + 1 = \${vetNum + 1}`)
``````

Also, add the following line just above the sort line, so you know when each function is called with new arguments.

``````console.log('\n', arr, num)
``````

is my updated code… now

``````arr.indexof(vetNum) // is not a function

``````

???

``````function getIndexToIns(arr, num) {
arr.sort();
let vetNum;
let vetter;

for(let i = 0; i < arr.length; i+= 1 ){
vetNum = arr[i];
vetter = arr[i+1]

if(num > vetNum && num < vetter ){
return arr.indexof(vetNum)
}
}

}

console.log(getIndexToIns([10, 20, 30, 40, 50], 35));

``````

includes updated console.logs with added variable vetter

``````function getIndexToIns(arr, num) {
console.log('\n', arr, num)

arr.sort();
let vetNum;
let vetter;

for(let i = 0; i < arr.length; i+= 1 ){
vetNum = arr[i];
vetter = arr[i+1]

console.log(`num = \${num}, vetNum = \${vetNum} vetter = \${vetter}`)

if(num > vetNum && num < vetter ){
return arr.indexof(vetNum)
}
}

}

console.log(getIndexToIns([10, 20, 30, 40, 50], 35));

``````

`indexof` is not a valid method name. JavaScript is case-sensiitive.

also tried assigning result to variable for a larger scope…

``````function getIndexToIns(arr, num) {
console.log('\n', arr, num)

arr.sort();
let vetNum;
let vetter;
let result;

for(let i = 0; i < arr.length; i+= 1 ){
vetNum = arr[i];
vetter = arr[i+1]

console.log(`num = \${num}, vetNum = \${vetNum} vetter = \${vetter}`)

if(num > vetNum && num < vetter ){
result = arr.indexof(vetNum)
return result
}
}

}

console.log(getIndexToIns([10, 20, 30, 40, 50], 35));

``````
``````arr.indexof(vetNum) // is not a function
``````

There is no such array method as `indexof`. You have a typo.

ok i see that … now? why is the sort effect not numerical…
i included a “post sort()” console.log and im now differently confused

``````function getIndexToIns(arr, num) {
console.log('\n', arr, num)

arr.sort();
console.log(arr)
let vetNum;
let vetter;
let result;

for(let i = 0; i < arr.length; i+= 1 ){
vetNum = arr[i];
vetter = arr[i+1]

console.log(`num = \${num}, vetNum = \${vetNum} vetter = \${vetter}`)

if(num > vetNum && num < vetter ){
result = arr.indexOf(vetNum)
return result
}
}

}

console.log(getIndexToIns([10, 20, 30, 40, 50], 35));

``````

You need a proper compare function. Without a compare function, the elements are converted to strings, so “10” would come before “4”.

can you possibly supply a tiiiiiny bit more instructional and exampling of a compare Fn… i m swriling around comprehension here… but am struggling to touch down
hahaha

like the raw step by step implementation of the compareFn itself… its architecture is not “obvious” to me yet?

Here’s an article on it