Why doesn't my code for Where do I Belong challenge work?

Why doesn't my code for Where do I Belong challenge work?
0

#1
function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  let sorted = arr.sort((a,b) => a - b);
  console.log(sorted);
  for(let x in sorted){
    if(sorted[x] >= num){
      return x;
    }
  }
  return sorted.length;
}

I wonder If anyone can tell me why this code doesn’t work. BTW I know that a simple for loop works and just want to learn why this one does not work FCC but works on Atom. Thanks!


Where do I belong (Possible FCC problem)
#2

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

Note: Backticks are not single quotes.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

I moved your question to a new topic, because you were asking about your own solution and not commenting or providing feedback to the original poster of the other thread.


#3

x is a string, because you you wrote let x in sortedinside your for loop. You should be returning a number.


#4

Thank you for the answer, sorry about the mess :smiley: