0

I don't know why the code skips some numbers some times. For case 4: a: [4, 2, 9, 11, 2, 16] my output is [2, 4, 2, 9, 11, 16] when the expected output is [2, 2, 4, 9, 11, 16].

I'm not modifying the index array so it should check every number position's. Also, I'm not sure how line 7 completely works, in position j it's added the elimination of the element i? If so why is there an [0] behind?

I find this code kind of messy but I need to learn from it.

function sortByHeight(a) {
    r = a
    for(i = 0; i < a.length; i++) {
        if (a[i]!= -1) {
            for(j = 0; j < a.length; j++) {
                if (a[j] != -1 && a[i] - a[j] < 0) {
                    r.splice(j,0,r.splice(i,1)[0])
                }
            }
        }
    }
    return r
}
underscore
  • 6,495
  • 6
  • 39
  • 78
dvorakk
  • 11
  • 8
    Any reason you're not using [Array.prototype.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)? Is this a learning experience on manually writing sorting algorithms? – Kelvin Schoofs Jul 24 '21 at 15:16
  • Does this answer your question? [Modifying a copy of a JavaScript object is causing the original object to change](https://stackoverflow.com/questions/29050004/modifying-a-copy-of-a-javascript-object-is-causing-the-original-object-to-change) – Sebastian Simon Jul 24 '21 at 15:17
  • 2
    That's a poor choice of code to learn from. It's a very inefficient sorting algorithm, and it's badly-implemented. Local variables are not declared. – Pointy Jul 24 '21 at 15:17
  • 1
    It's because you are using `splice()` on the original array which is destructive. You'll need create a new array and populate it instead. – vanowm Jul 24 '21 at 15:17
  • If you are not sure how it works (or why it doesn't), split it into a bit smaller chunks (for example putting the "nested" splice into a separate variable) and then use a debugger to walk through the code line by line as it executes and look at all the variables and how they change on each step – CherryDT Jul 24 '21 at 15:24
  • Briefly answering to all of you. I'm not using sort because that would change the whole array. I want the ```-1``` to stay put wherever they are. I don't think forgetting to write ```let``` makes this not suitable for learning from, maybe you are on another level. The intention wasn't to use splice() on the original array, I thought I had made a copy. Thanks for the comments though. – dvorakk Jul 24 '21 at 20:14

2 Answers2

0

Your mistake is probably thinking that r = a copies the array. That's not the case in JavaScript. JavaScript works by reference, meaning that every value (apart from primitives such as numbers) are actually references. Therefore, r and a point to the exact same array in memory. Modifying one also "modifies the other", so to speak.

You can clone the array by doing one of the following:

r = [ ...a ];
r = a.slice();
r = Array.from(a);
Kelvin Schoofs
  • 8,323
  • 1
  • 12
  • 31
  • Thanks for teaching me this, I had forgotten. The thing is the code actually works worse now hahah, I'll wait for more help I guess. – dvorakk Jul 24 '21 at 20:17
0

your mistake is in this line (a[j] - a[i] < 0), you checking it for (a[i] - a[j] < 0), checking in the reverse order will give you the array in decreasing order, you can reverse the array before returning will give you it in increasing order

function sortByHeight(a) {
r = a
for(i = 0; i < a.length; i++) {
    if (a[i]!= -1) {
        for(j = 0; j < a.length; j++) {
            if (a[j] != -1 && (a[j] - a[i] < 0)) {
                r.splice(j,0,r.splice(i,1)[0])
            }
        }
    }
}
r.reverse()
return r
}
Anonymous Coder
  • 556
  • 2
  • 16
  • No, I don't know why you think ```(a[j] - a[i] < 0)``` makes any difference. Actually the splice positions don't make sense that way, that's not the problem. – dvorakk Jul 24 '21 at 20:10