使用JavaScript进行简单的计算

时间:2014-03-07 02:18:47

标签: javascript

我(总Javascript Noob)我正在尝试编写一个javascript来为通用航空飞机创建一个重量和平衡计算器。

但是我一路上都被困住了。 用户应输入重量信息,计算器应计算飞机的力矩和/或重量,并总结相应的字段。

不幸的是我无法让脚本计算用户输入。有没有人有建议?

我的HTML是:( JS在下面进一步说明)

<form id="form" onsubmit="return false;">
<table>
    <tr>
        <th>Station</th>
        <th>Weight</th>
        <th></th>
        <th>Arm</th>
        <th></th>
        <th>Moment</th>
        <th></th>
    </tr>
    <tr>
        <td>Empty Weight</td>
        <td>
            <script type="text/javascript">
                document.write(weight);
            </script>
        </td>
        <td>kg</td>
        <td>
            <script type="text/javascript">
                document.write(acarm);
            </script>
        </td>
        <td>kg-m</td>
        <td>
            <script type="text/javascript">
                document.write(moment);
            </script>
        </td>
        <td>m</td>
    </tr>
    <tr>
        <td>Pilot</td>
        <td>
            <input type="text" value="0" onkeypress="return isNumberKey(event)" onblur="onBlur(this)" onfocus="onFocus(this)" id="Pilot" />
        </td>
        <td>kg</td>
        <td>
            <script type="text/javascript">
                document.write(afseat);
            </script>
        </td>
        <td>kg-m</td>
        <td>
            <script type="text/javascript">
                document.write(pimom);
            </script>
        </td>
        <td>m</td>
    </tr>
    <tr>
        <td>CoPilot</td>
        <td>
            <input type="text" value="0" onkeypress="return isNumberKey(event)" onblur="onBlur(this)" onfocus="onFocus(this)" id="CoPilot" />
        </td>
        <td>kg</td>
        <td>
            <script type="text/javascript">
                document.write(afseat);
            </script>
        </td>
        <td>kg-m</td>
        <td>
            <script type="text/javascript">
                document.write(cpmom);
            </script>
        </td>
        <td>m</td>
    </tr>
    <tr>
        <td>Pax 1</td>
        <td>
            <input type="text" value="0" onkeypress="return isNumberKey(event)" onblur="onBlur(this)" onfocus="onFocus(this)" id="Pax1" />
        </td>
        <td>kg</td>
        <td>
            <script type="text/javascript">
                document.write(arseat);
            </script>
        </td>
        <td>kg-m</td>
        <td>
            <script type="text/javascript">
                document.write(p1mom);
            </script>
        </td>
        <td>m</td>
    </tr>
    <tr>
        <td>Pax 2</td>
        <td>
            <input type="text" value="0" onkeypress="return isNumberKey(event)" onblur="onBlur(this)" onfocus="onFocus(this)" id="Pax2" />
        </td>
        <td>kg</td>
        <td>
            <script type="text/javascript">
                document.write(arseat);
            </script>
        </td>
        <td>kg-m</td>
        <td>
            <script type="text/javascript">
                document.write(p2mom);
            </script>
        </td>
        <td>m</td>
    </tr>
    <tr>
        <td>Fuel</td>
        <td>
            <input type="text" value="0" onkeypress="return isNumberKey(event)" onblur="onBlur(this)" onfocus="onFocus(this)" id="Fuel" />
        </td>
        <td>l</td>
        <td>
            <script type="text/javascript">
                document.write(abag);
            </script>
        </td>
        <td>kg-m</td>
        <td>
            <script type="text/javascript">
                document.write(bamom);
            </script>
        </td>
        <td>m</td>
    </tr>
    <tr>
        <td>Baggage</td>
        <td>
            <input type="text" value="0" onkeypress="return isNumberKey(event)" onblur="onBlur(this)" onfocus="onFocus(this)" id="Baggage" />
        </td>
        <td>kg</td>
        <td>
            <script type="text/javascript">
                document.write(abag);
            </script>
        </td>
        <td>kg-m</td>
        <td>
            <script type="text/javascript">
                document.write(bamom);
            </script>
        </td>
        <td>m</td>
    </tr>
    <tr>
        <td>Total</td>
        <td>
            <script type="text/javascript">
                document.write(totweight);
            </script>
        </td>
        <td>kg</td>
        <td>
            <script type="text/javascript">
                document.write(totarm);
            </script>
        </td>
        <td>kg-m</td>
        <td>
            <script type="text/javascript">
                document.write(totmom);
            </script>
        </td>
        <http://jsfiddle.net/#savetd>m</td>
    </tr>
    <tr>
        <td></td>
        <td>
            <input type="submit" onclick="weightandbalance();" />
        </td>
        <td></td>
        <td></td>
    </tr>
</table>

这是JS:

     // Basic Aircraft Set Up
    var weight = 627.9; // Basic Empty Weight in kg
    var moment = 201.22; // Moment in m-kg
    var acarm = Math.round((moment / weight) * 10000) / 10000;

    var mtow = 1002; // Maximum Take Off Weight in kg
    var afseat = 0.41; // Front Seat Arm in m
    var arseat = 1.19; // Rear Seat Arm in m
    var abag = 1.9; // Baggage Area Arm in m
    var afuel = 1.12; // Fuel Tank Arm in m


function weightandbalance() {

    var Pilot = parseFloat(document.getElementById("Pilot").value);
    var CoPilot = document.getElementById("CoPilot").value;
    var Pax1 = document.getElementById("Pax1").value;
    var Pax2 = document.getElementById("Pax2").value;
    var Baggage = document.getElementById("Baggage").value;
    var Fuel = document.getElementById("Fuel").value;

    // Perform Calculations
    var pimom = Pilot * afseat;
    var cpmom = CoPilot.value * afseat;
    var p1mom = Pax1.value * arseat;
    var p2mom = Pax2.value * arseat;
    var bamom = Baggage.value * abag;
    var fumom = Fuel.value * afuel * 0.71;

    var totweight = weight + Pilot + CoPilot + Pax1 + Pax2 + Baggage + Fuel;
    var totmom = moment + pimom + cpmom + p1mom + p2mom + bamom + fumom;
    var totarm = Math.round((totmom / weight) * 10000) / 10000;



}

// Allow only numbers
function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) return false;

    return true;
}


//Default Values    
function onBlur(el) {
    if (el.value == '') {
        el.value = el.defaultValue;
    }
}

function onFocus(el) {
    if (el.value == el.defaultValue) {
        el.value = '';
    }
}

1 个答案:

答案 0 :(得分:1)

您可能希望在某个地方的代码中放置一个console.log()语句,然后打开浏览器的开发人员控制台,看看它打印出来的内容。

例如,在计算totweight或其他内容后将其放置:

var totweight = weight + Pilot + CoPilot + Pax1 + Pax2 + Baggage + Fuel;
console.log(totweight);

如果您使用的是Chrome,则可以按 Ctrl Shift J (Windows)或 Cmd <打开控制台/ kbd> 选择 J (Mac)。

您可能想要做的另一件事是对您的其他变量执行parseFloat(),就像您对Pilot变量所做的那样。您可能会在此处的代码段中找回字符串而不是数字。所以你可以这样做:

console.log(typeof CoPilot);

查看控制台在浏览器中输出的内容。如果它打印出“string”而不是“number”,你也会知道ParseFloat()那些变量。

您还可以执行以下操作以检查CoPilot(或任何其他变量)是否为非数字:

console.log( isNan(CoPilot) );

我希望这会有所帮助。