Passing 10/16 of the challenges on this assignment

Tell us what’s happening:

EDIT: Solved. Never mind

Hey folks,

So I thought I had solved this. I’ve searched, I’ve used console.log() but I can’t understand why I’m not passing this assignment. I pass most of the tests, but there a few select parameters that when passed into my function do not return the correct index. I’m including one of those sets below. What am I doing wrong?


Your code so far

function getIndexToIns(arr, num) {
let value = "";
arr.sort((a, b) => a - b);
for (let i = 0; i < arr.length; i++) {
  if (num >= arr[i]) {
   value = arr.indexOf(arr[i]);

  return value;

getIndexToIns([40, 60], 50);

Your browser information:

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

Challenge: Where do I Belong

Link to the challenge:

@miles Glad to see you were able to solve the problem you were facing. Just as an extra bit of information to anyone interested, JavaScript allows for this challenge to be passed without explicitly writing a for loop. As an extra challenge, see if you can get it to pass with only 3 lines of code inside the function (including the return statement) and only using the JavaScript methods push, sort, and indexOf.


Wow, I was able to do it with just three lines of code and those methods.

At first, I thought this was crazy, but I’m stuck on another challenge and thought, “why not try!”.

Thanks for the prompting :slight_smile:

EDIT: decided to remove the code in case someone else wants to try it without the solution sitting right here.

1 Like

it’s also solvable without sorting, with just a loop

which should maybe be the most performant way as you are iterating over the array just once

Good point. It’s always a trade off with JavaScript - ease of coding vs performance.