Write a function in JS to calculate the size of three different numbers and print them in ascending order (exhaustive method)

Write a function to calculate the size of three different numbers, and print the exhaustive method in ascending order

<body>
    <input type="text" id="txt1">
    <input type="text" id="txt2">
    <input type="text" id="txt3">
    <input type="button" id="btn" value="比较">
    <input type="text" readonly id="res">
</body>
<script>
    var txt1 = document.getElementById("txt1");
    var txt2 = document.getElementById("txt2");
    var txt3 = document.getElementById("txt3");
    var btn = document.getElementById("btn");
    var res = document.getElementById("res");


    btn.onclick = function(){
    
    
        var n1 = txt1.value * 1;
        var n2 = txt2.value - 0;
        var n3 = Number(txt3.value);

        var str = "";

        // 列出所有可能性
        if(n1 > n2 && n1 > n3){
    
    
            if(n2 > n3){
    
     
                str = "" + n1 + n2 + n3;
            }else{
    
    
                str = "" + n1 + n3 + n2;
            }
        }else if(n2 > n1 && n2 > n3){
    
    
            if(n1 > n3){
    
    
                str = "" + n2 + n1 + n3;
            }else{
    
    
                str = "" + n2 + n3 + n1;
            }
        }else if(n3 > n1 && n3 > n2){
    
    
            if(n1 > n2){
    
    
                str = "" + n3 + n1 + n2;
            }else{
    
    
                str = "" + n3 + n2 + n1;
            }
        }
        res.value = str;
    }


</script>

Guess you like

Origin blog.csdn.net/qq_26705343/article/details/111356600