Basic Algorithm Scripting - Where do I Belong

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:

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

arr.indexof(vetNum)

why does the if statement get ignored

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));




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

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));


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

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.