Ian.M
March 5, 2023, 7:11pm
1
why does it keep coming back undefined… am i messing up syntactically somewhere?
Your code so far
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));
Your browser information:
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
Link to the challenge:
Ian.M:
arr = arr.sort()
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.
Ian.M
March 5, 2023, 7:18pm
4
Ian.M:
arr =
cool cool thanks… but why is my code itself returning undefined instead of
arr.indexof(vetNum)
Ian.M
March 5, 2023, 7:19pm
5
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)
Ian.M
March 5, 2023, 7:34pm
7
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));
Ian.M
March 5, 2023, 7:48pm
8
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.
Ian.M
March 5, 2023, 7:53pm
10
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
Ian.M:
arr.indexof(vetNum)
There is no such array method as indexof
. You have a typo.
Ian.M
March 5, 2023, 7:57pm
12
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”.
The sort() method sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code...
Ian.M
March 5, 2023, 8:06pm
14
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
Ian.M
March 5, 2023, 8:08pm
15
like the raw step by step implementation of the compareFn itself… its architecture is not “obvious” to me yet?