如何使我的转换器按降序排列

时间:2022-02-12 01:12:36

I have created a conversion table which converts miles to kilometres and kilometres to miles depending on whichever one the user chooses. They input two numbers which indicates the two ranges so if they input 2 and 5 and choose km to m it will then show 2km to 5km converted to miles. However, what I am trying to do is if the user inputs a greater number to start with for instance if you enter 10 and 2 it should still do the same but rather it should go from 10km down to 2km so in descending order, so I know it will be something along the lines of if(rangeStart>rangeEnd){i--;}

我创建了一个转换表,根据用户选择的转换表将里程转换为千米和千米。他们输入两个数字,表示两个范围,所以如果他们输入2和5并选择km到m,那么它将显示2km到5km转换为英里。但是,我想要做的是,如果用户输入一个更大的数字开始,例如,如果你输入10和2它仍然应该做同样的,但它应该从10公里下降到2公里,所以按降序排列,所以我知道它将是if(rangeStart> rangeEnd){i--;}的内容

<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

<script>
function conversion(n) {
if (document.getElementById('mtokm').checked) {
    return (n/0.62137).toFixed(2);
}
else {
return (n*0.62137).toFixed(2);
}
    }

function conversionTable(rangeStart, rangeEnd) {
if(atLeastOneRadio() && rangeStart != false && rangeEnd != false) {

divStr="<table border=1><tr><td>Miles</td><td>Kilometres</td></tr>";}


    for(i=rangeStart;i<=rangeEnd;i++) {
    if(i%2==0)
    {
        divStr+= "<tr bgcolor=\"yellow\"><td>" + i + "</td><td>" + conversion(i) + "</td></tr>";
    }
    else
    {
        divStr+= "<tr bgcolor=\"green\"><td>" + i + "</td><td>" + conversion(i) + "</td></tr>";
    }
  }
    document.getElementById("divResult").innerHTML=divStr;
 }
  else
  {
alert("Please make sure you have entered an integer in both text boxes");
  }
}

function getnputValue(input) {
var nn = $("input[name=convert]:checked").val()
var myInt = document.getElementById(input).value;
if(myInt == parseInt(myInt))
return parseInt(myInt);
else
    return false;

}




function check() {
    var radios = document.getElementsByName("choice");
    $("input[name=convert]:checked").val()
    for (var i = 0, len = radios.length; i < len; i++) {
    if (radios[i].checked) {
        return true;
}
}

return false;
}

function atLeastOneRadio() {
    return ($('input[type=radio]:checked').length > 0);

}
</script>
</head>
<body>
<p>
    Start : <input type=textbox id=rangeTxt value=""/>
    End : <input type=textbox id=rangeTxt2 value=""/>
    <input type=radio name="convert" id="mtokm" value ="Miles to Kilometre"/>        Miles to Kilometre
    <input type=radio name="convert" id="kmtom" value ="Kilometre to Miles"/>  Kilometre to Miles
    <br>
    <br>
    <button onClick="conversionTable(getnputValue('rangeTxt'),        getnputValue('rangeTxt2'))">Convert</button>
    </p>
    <div id="divResult">
    </div>
    </body>
    </html>

1 个解决方案

#1


2  

Check whether the end is higher or lower than the start. Then set variables that are used to control the for loop.

检查结束是否高于或低于开始。然后设置用于控制for循环的变量。

var increment, compare;
if (rangeStart <= rangeEnd) {
    increment = 1;
    compare = function(x, y) {
        return x <= y;
    };
} else {
    increment = -1;
    compare = function(x, y) {
        return x >= y;
    };
}

for (i = rangeStart; compare(i, rangeEnd); i += increment) {
    // display code
}

#1


2  

Check whether the end is higher or lower than the start. Then set variables that are used to control the for loop.

检查结束是否高于或低于开始。然后设置用于控制for循环的变量。

var increment, compare;
if (rangeStart <= rangeEnd) {
    increment = 1;
    compare = function(x, y) {
        return x <= y;
    };
} else {
    increment = -1;
    compare = function(x, y) {
        return x >= y;
    };
}

for (i = rangeStart; compare(i, rangeEnd); i += increment) {
    // display code
}