Hi guys, I was just not passing the last two tests so I noticed that the reason was I was placing else {return arr.length } as part of the if part function like so . Why is it in the outerlayer?
for (let i = 0; i < arr.length; i++) {
if (num <= arr[i]) {
return i;
} else {return arr.length
}
}
}
**Your code so far**
function getIndexToIns(arr, num) {
var sortedArr = arr.sort();
function compareNumbers (a,b) {
return a - b;
}
sortedArr.sort(compareNumbers);
for (let i = 0; i < arr.length; i++) {
if (num <= arr[i]) {
return i;
}
}
return arr.length;
}
console.log(getIndexToIns([2, 5, 10], 15));
// re-order arr
// loop through arr to find at what index is num smaller than arr[i]
// return the index as a result
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36.
Well… there are cases where you want a return in a for loop, where you want to stop execution of the loop and just return out the function because you’ve found/done what you want.
First of all, please learn to format your code - it makes it so much easier to tell what is going on.
And this won’t work because it will bail on the first iteration. If your array and number are [2, 5, 10] and 3, it will compare the first one, see that 3 is not less or equal to 2 and will go to the else - game over. This code will never get passed the first element, no matter what.
Also, in your code, what do you think this line is doing?
var sortedArr = arr.sort();
I guess it is not hurting anything, but I also don’t think it is doing what you think it is doing.
I followed the mozilla docs which is why it’s not arr.sort(function(a, b))... in one line. I was still able to get the right answer with my code though once I removed the return and placed it in the outer bracket.
At first I thought
for (var i = 0; i < arr.length; i++) {
if (arr[i] >= num) return i;
}
return arr.length;
and
for (let i = 0; i < arr.length; i++) {
if (num <= arr[i]) {
return i;
} else {
return arr.length
}
were the same since writing else is not always needed but I see that it is not. Can you perhaps send a link to read more into this? I read the if…else mozilla doc and wasn’t able to see learn more about this.
is going to sort arr but because you have provided no callback, it will sort it as strings, not as numbers, e.g., 10 will come before 2. There is also no need to do that here since you sort it correctly a few lines later. All this line does is sort arr (incorrectly) and then creates a new variable that points to the new array. I would argue that you don’t even really need the sortedArr variable since you are mutating arr anyway.
As to understanding how an if/else works, I don’t know what to say because I’m not sure what you are understanding. Look at it in pseudocode
loop through 10 elements
if some_condition
return out of function with A
else
return out of function with B
That is always going to run 1 iteration of the loop, no matter what. Either some_condition is going to be true and it with return with A or it won’t and the function will return with B. But no matter what, on that first iteration, a return is going to happen, so the loop will never get to a second iteration.