• + 0 comments

    Can someone help me understand why my program, written in JavaScript programming language, failed some test cases in solving bigSort challenge?

    First of all (or scenario 1), I solved the "bigSort challenge" with application of bubble sort algorithms:

    function bigSort(unsorted) { const size = unsorted.length;

    for (let i = 0; i <= size - 1; i++) {
        let isSwapped = false;
    
        // Compare two values at a time, place
        // the bigger value at the right side of the array
        // and place the lower value at the left side of
        // array.
        for (let j = 0; j <= size - 1; j++) {
            if (parseInt(unsorted[j + 1]) < parseInt(unsorted[j])) {
    
                // Temporarily hold the least value.
                const minValue = unsorted[j + 1];
    
                unsorted[j + 1] = unsorted[j];
                unsorted[j] = minValue;
    
                // Track and update whenever some value are swapped.
                isSwapped = true;
            }
    
            if (!isSwapped) break;
        }
    
        return unsorted;
    }
    

    To my surprise, this fails test: test case 2, test case 7 and some others.

    Secondly (or scenario 2), I modified the code with the language in built "sort()" method. I provided it with the callback compare function as follows:

    functiion bigSort(unsorted) {
        return unsorted.sort((a, b) => a.localeCompare(b, undefined, {numeric: true});
    }
    

    I am surprised when I see test case 2 and 7 marked failed.

    Folks, kindly help me understand why some test cases failed, specifically test case 2 and 7 in the two scenarios.