Basic Algorithm Scripting - Where do I Belong

Tell us what’s happening:

Trying to tackle this problem and the second for loop seems not to work.
What I’m trying to do is:

1st for loop :

  1. get the Math.min among the spreaded(...) inputted array
  2. get the index of that minimum (indexOf)
  3. make a new array, “orderedArr” and push the minimum, continuing until
    inputted array’s length is 0

then in 2nd loop :

  1. use for (let a in b) to check each items in ‘orderedArr’
  2. using if target the number which value is bigger than inputted num,
    (here i’m assuming that iteration starts from left to right)
  3. then using splice() insert the inputted num into ‘orderedArr’
  4. finally return numInd, the index num has taken place after previously taken by bigger number

when I try to input given example, undefined is given…

Your code so far

function getIndexToIns(arr, num) {
  const orderedArr = [];
 if (arr === []) {
    return 0;
  }

  for (; arr.length != 0;) {
    let miniInd = arr.indexOf(Math.min(...arr));
    orderedArr.push(...arr.splice(miniInd, 1));
  }

  for (let numInd in orderedArr) {
    if (num < arr[numInd]) {
    orderedArr.splice(numInd, 0, num);
    return numInd;
    }
  }
}

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/108.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Where do I Belong

Link to the challenge:

There are some JS issues here.

First of all:

if (arr === []) {

…does not do what you think it does. Reference types are compared by reference (memory address). What this is asking is, “Is the reference for arr the same as the reference for this anonymous empty array literal that I created just for the purpose of this test?” That will never be true.

for (; arr.length != 0;) {

This is bad JS. That is just a while loop in disguise.

Wait, is that first look just trying to do a sort? There is a method for that.

Is that the array you want to be checking it against?

for (let numInd in orderedArr) {

for…in is usually used for iterating it over objects, so it is looking for properties, not indexes. It will work, but indexes are numbers and properties names are strings, so …

If you searched the array methods on MDN, you would find methods that could do each of these loops for you. You definitely should for the first one. For the second one, if you must, I would just use a standard for loop, with indexes.

then how should i express the equality?
or should i set extra variabe such as (let x = [ ]) and the compare equaity?

first loop is just trying to make unordered array into ordered array
for example
[1, 5, 2] to [1,2,5]

oh nevermind, i can use .length to check if it’s 0

1 Like

Yes, we call that “sorting”. There is an array method that will do that for you. What you have works, but it is not the ideal way to do it. Yes, you can use a knife as a screwdriver, but it is not the right tool for the job and is a bad habit. Those prototype methods are very valuable and powerful. But beware, there is a “gotcha” for using the sort method with numbers - read the documentations. Google “MDN array sort”.

2 Likes