Tell us what’s happening:
I’m getting the correct answer on the console but its isnt passing… I just noticed the [] test at the bottom to which my code returns “undefined”. Could this be negating the correct answers at the top?
Your code so far
function getIndexToIns(arr, num) {
var answer;
arr.sort(function(a, b) {
return a - b});
for (var i = 0; i <arr.length; i++) {
if (num > arr[i]) {
answer = [i+1]}
}console.log(answer);
}
getIndexToIns([40, 60], 50);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36.
I’m doing the console.log because when I return, it marks all results as fails. The only "undefined " result is on the empty array at the VERY bottom. I left “c.log” in my post to illustrate how I am verifying the output. Can you tell me why the getIndexToIns([40, 60], 50); returns [1] as required and getIndexToIns([10, 20, 30, 40, 50], 35); yields a [ 3 ] as required but fails this part of the test? I had written a line before I asked for help that went something like else if (arr = []) {answer = 0;} or else (arr ! = truthy). still all fails…just now i reset the code and passed half of them. The above code returns the right answer and gets all X’s. Its interesting, right?