Tell us what’s happening:
For this challenge I have to return the index position where num would fit in the sorted array.
I’m trying to incorporate the indexOf method in my solution, but what I’m coming up with isn’t working and I’d like some help with figuring out why. To explain my thinking process, I wanted to first create a variable that contained the sorted array from least to greatest. Then create a variable to contain the index where num will go. So I created a For loop to index through the sorted array, and tried to return the indexOf sortedArr[i] that is greater than num.
EDIT 1: I just realized the first problem was having the For loop condition set to i > sortedArr.length which means it would never execute. I changed that and now its saying sortedArr[i].indexOf is not a function, which I’m working to resolve.
**Your code so far**
function getIndexToIns(arr, num) {
let sortedArr = arr.sort();
let greaterThanIndex = 0;
for (let i = 0; i < sortedArr.length; i++) {
return greaterThanIndex = sortedArr[i].indexOf(sortedArr[i] > num)
} return greaterThanIndex
}
console.log(getIndexToIns([40, 60, 80, 20, 10, 11], 50))
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36
.
Challenge: Where do I Belong
Link to the challenge: