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
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
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));
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
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));
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?
system
Closed
September 4, 2023, 11:42am
17
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.