why does this merge sort code work in python but not in javascript

Does anyone know why this code isn’t working, I tried the same code in python and it worked perfectly.

function merge(l,m,r) {
            let divider = m+1
            let l_cache = l
            let  out_list = []
            for (let i = 0; i < r-l+1; i++){
                if (l > m){
                    out_list.push(numbers[divider])
                    divider += 1
                }else if(divider > r){
                    out_list.push(numbers[l])
                    l += 1
                }else if(numbers[l] < numbers[divider]){
                    out_list.push(numbers[l])
                    l += 1
                }else if(numbers[l] >= numbers[divider]){
                    out_list.push(numbers[divider])
                    divider += 1
                }
            }
            for (let i = 0; i < out_list.length; i++){
                numbers[i+l_cache] = out_list[i]
            }
        }
    function MergeSort(l,r) {
        if (l < r){
            let m = Math.floor((l+r-1)/2)
            MergeSort(l,m)
            MergeSort(m+1,r)
            merge(l,m,r)
        }
      }
    var numbers = [42, 60, 33, 79, 15, 0, 88, 62, 27, 46]
    MergeSort(0,numbers.length-1)

input = [42, 60, 33, 79, 15, 0, 88, 62, 27, 46]

output = [0, 15, 27, 33, 42, 46, 60, 46, 62 ,62]

expected output = [0, 15, 27, 33, 42, 46, 60, 62, 79, 88]

26 thoughts on “why does this merge sort code work in python but not in javascript”

Leave a Comment