Where do i belong basic algorithm challenge

Tell us what’s happening:
I need help to know why my code does not return the index, so far the loop arranges the elements in array in ascending order but the second loop doesn’t seem to bring the right answers in all the tests. It also doesn’t bring the right counts

Your code so far


function getIndexToIns(arr, num) {
// Find my place in this sorted array.
let newArr=[];
let count=0;
for(var i=0; i<arr.length; i++){
  let largerNum=arr[0];
  if(largerNum<arr[i]){
    newArr[0]=largerNum;
    newArr.push(arr[i]);
    console.log(newArr);
  }
 
 for (var j=0; j<arr.length; j++){
   if(num>newArr[j]){
     count++;
   }
 }
    }
  
return count++;

}

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

Your browser information:

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

Challenge: Where do I Belong

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/where-do-i-belong

1 Like

I used an outo-formatter to tidy up your code
when the second loop should run? when does it run?

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  let newArr = [];
  let count = 0;
  for (var i = 0; i < arr.length; i++) {
    let largerNum = arr[0];
    if (largerNum < arr[i]) {
      newArr[0] = largerNum;
      newArr.push(arr[i]);
      console.log(newArr);
    }

    for (var j = 0; j < arr.length; j++) {
      if (num > newArr[j]) {
        count++;
      }
    }
  }

  return count++;

}

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

an hint to make things easier: you are counting how many numbers are lower than the number to put in - to count them do they need to be in a specific order?

The outcome of ieahleen code is
[ 10, 20 ]
[ 10, 20, 30 ]
[ 10, 20, 30, 40 ]
[ 10, 20, 30, 40, 50 ]
8
is that what you want?

Furthermore you have an error on the Global frame. To be more precies has to do with spaces and order.
This part gave me an error message

return count++;

i wanted to know whether this approach would also work, my main aim was to arrange them in order and just get the index of the position where they would fit

i wanted a single array, that output was not my desirable outcome, how do i rectify this?

that not your output , that’s what the console log prints at each iteration

to see the output you need to wrap the function call in a console log
to see the final value of a variable
you need to wrap it in a console.log after the changes have finished
I suggest you add a label to your console logs to have more infos on what is being printed in the console
like console.log("array inside loop", JSON.stringify(newArr)) (the stringify is to have the array print as an array whenever you are using it, the fcc console has a weird behaviour)

i dont understand this truthfully

nothing at all?

what is it that you don’t understand in particular?

if you want we can schedule a time to take a look at it together using the multiplayer mode of repl.it

first is how to wrap the function call in the console.log and second is i dont know anything about JSON, we can schedule time, i would appreciate that

at this time you don’t need to know much about JSON - just know that whatever you pass in JSON.strigify() is converted in a string.

console.log(func()) here an example of a function call passed in the console log method, it will print to the console the output of the function

you can PM me for scheduling a time

1 Like

I researched on funtion calls, can say am a little more knowledgeble, about the scheduling, i will

first part

first you have to sort array. sorting an array , check for sorting algorithm .
I prefer to use bubble sort algorithm. try to impediment that. if not , I think this challenge allow you to use already defined sorting method in JavaScript. one you ensure that, your array is sorted, then you have to check four condition.
second part

first condition, if array is empty, it should return 0

second condition, getIndexToIns([10, 20, 30, 40, 50], 35) should rerun index as 3
because 35 is less than 40. so you should rerun index of 40 . that is three
third condition
getIndexToIns([10, 20, 30, 40, 50], 30) should return 2
because index of 30 in array is 2

last condition
getIndexToIns([2, 5, 10], 15) should return 3
because 15 is not a value in the array. so just return length of array
posting this explanation after i wrote my own code and get the challenge passed

1 Like

i appreciate this, thanks

You do not have to sort the array to solve this challenge.

it is clearly mention in the challenge itself to sort first argument . I did not understand why you told me that it should not be sorted. below is my working solution

var numBerInArray=false

function getIndexToIns(arr, num) {

let sortedArrays=sortArray(arr)

if(arr.length===0){

return 0

}

else{

for(var i=0;i<sortedArrays.length;i++){

if(sortedArrays[i]>=num){

console.log(i)

numBerInArray=true

return i

}

else if(i===sortedArrays.length-1){

if(numBerInArray===false){

return arr.length

}

}

}

}

}

function sortArray(sorts){

for(var i=0;i<sorts.length;i++){

for(var j=i;j<sorts.length;j++){

if(sorts[i]>sorts[j]){

var k=sorts[i]

sorts[i]=sorts[j]

sorts[j]=k

}

}

}

return sorts

}

getIndexToIns([2, 5, 10], 15)

The challenge does not say you have to actually sort the array. It says you are to return the lowest index at which a value should be inserted into an array once it has been sorted. This means if you were to sort the array, you are to return the lowest index at which the value should be inserted into it.

Yes, you can solve the challenge by sorting the array first and applying the steps you mention above, but it can also be solved without ever sorting the array.

yes you are right . there is no need of sorting . only need to return the number of element in first argument(array) which is lesser than the second argument

1 Like