Sorting algorithm in Javascript

后端 未结 5 556
情深已故
情深已故 2021-01-29 09:23

Write a JavaScript callback for the jQuery function $(\"#sort\").click. Allow the user to enter three numbers in any order. Output the numbers in ord

5条回答
  •  北海茫月
    2021-01-29 09:55

    Try

    $("#sort").on("click", function (e) {
        var vals = $.map($("#a, #b, #c"), function (v, k) {
            return Number(v.value)
        })
        , min = null
        , msg = "";
        do {
            min = Math.min.apply(Math, vals);
            msg += min;
            vals.splice($.inArray(min, vals), 1)
        } while (vals.length > 0);
        $("output").html(msg)
    });
    

        $("#sort").on("click", function (e) {
            var vals = $.map($("#a, #b, #c"), function (v, k) {
                return Number(v.value)
            })
            , min = null
            , msg = "";
            do {
                min = Math.min.apply(Math, vals);
                msg += min;
                vals.splice($.inArray(min, vals), 1)
            } while (vals.length > 0);
            $("output").html(msg)
        });
    
    
    
    
    
    

提交回复
热议问题