Where do I Belong. Hi everyone I would some help on why I am unable to pass this challenge

Tell us what’s happening:
Describe your issue in detail here.

Where do I Belong. Hi everyone I would some help on why I am unable to pass this challenge with the code I have provided as it outputs the correct answer every time, thanks.


function getIndexToIns(arr, num) {
let arr1 = []
let res = ''
arr.sort(function(a, b) {
  return a - b;
});
arr1.push(num)
for (let i = 0; i < arr.length; i++) {
  if (arr[i] < num) {
    arr1.unshift(arr[i])
  } else if (arr[i] > num) {
    arr1.push(arr[i])
  }
  arr1.sort(function(a, b) {
    return a - b;
  });
}
for (let j = 0; j < arr1.length; j++){
  if (arr1[j] === num){
    res += j
  }
}
return res
}
console.log(getIndexToIns([20,3,5], 19));

  **Your browser information:**

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

Challenge: Where do I Belong

Link to the challenge:

Is res really supposed to be a string? I’d look into what happens when you use the plus sign with both a number and a string.

I’m unsure but I have read it again and it has to return a number so I that’s what the problem it, I just have to figure out how to get my result to return a number not a string. Thanks for replying

I would suggest you declare it as a number and not a string then. Once you do that your code will pass.

I’m still new and it might be a dumb question but how would I do that?

Right now you are initializing the res variable to an empty string:

let res = ''

The equivalent of this for a number would be to initialize it to 0.

You’re legend man, it passed thanks so much for the help.

1 Like

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