javascript音量计算器从宽度,长度和深度 [英] javascript volume calculator from width, length and depth

查看:113
本文介绍了javascript音量计算器从宽度,长度和深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对数学并不陌生,我必须承认:)

I'm not amazing at maths, I must admit :)

基本上我需要创建一个计算器:

Essentially I need to create a calculator that takes:

1. width
2. length
3. depth

从这些输入中,我需要以m³显示答案。

From these inputs, I need to display the answer in m³.

使用它可以选择5个下拉选项:

Making it a little more tricky, a user can choose between 5 drop-down options:

1. Centimeter
2. Inches
3. Feett
4. Yards
5. Meters

例如,用户可以执行以下操作:

For example, a user could do something like:

width = 10 centimeters
length = 7 foot
depth = 2 inches

所以我的想法是转换所有用户输入分为毫米,使它们成为同一类型。查找交易量的公式为:

So my thought was to convert all user input into millimeters to make them the same type. The formula for finding a volume is:

length * height * width 

所以我想如果我以毫米为单位,我可以将其转换回米。但是,我遇到了问题而没有从我的代码中得到正确的答案。逻辑必须完全错误(就像我说我的数学并不惊人)

So I figure if I do this in millimeters, I can then convert it back to meters. However, I'm running into problems and not getting the correct answers from my code. The logic must be completely wrong (Like I said my maths is not amazing )

这是我的代码:

    <tr>
        <td>Width</td>
        <td><input type="text" id="width"/></td>
        <td>
            <select id="width-type">
                <option value="centimeter">Centimeter</option>
                <option value="inches">Inches</option>
                <option value="feet">Feet</option>
                <option value="yards">Yards</option>
                <option value="meters">Meters</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>Length</td>
        <td><input type="text" id="length"/></td>
        <td>
            <select id="length-type">
                <option value="centimeter">Centimeter</option>
                <option value="inches">Inches</option>
                <option value="feet">Feet</option>
                <option value="yards">Yards</option>
                <option value="meters">Meters</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>Depth</td>
        <td><input type="text" id="depth"/></td>
        <td>
            <select id="depth-type">
                <option value="centimeter">Centimeter</option>
                <option value="inches">Inches</option>
                <option value="feet">Feet</option>
                <option value="yards">Yards</option>
                <option value="meters">Meters</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>
            <button id="calculate">Calculate</button>
        </td>
    </tr>



<script>
$('#calculate').click(function(){

//These are the amount of mm in each drop down menu type
var array = new Array();
array['centimeter'] = '10';
array['inches']     = '25.4';
array['feet']       = '304.8';
array['yards']      = '914.4';
array['meters']     = '1000';

//Find the width in mm
var widthVal    = $('#width').val();
var widthType   = $('#width-type').val();
var width       = widthVal * array[widthType];

//Find the length in mm
var lengthVal   = $('#length').val();
var lengthType  = $('#length-type').val();
var length      = lengthVal * array[lengthType];

//Find the depth in mm
var depthVal    = $('#depth').val();
var depthType   = $('#depth-type').val();
var depth       = depthVal * array[depthType];

//Find the total volume in mm
var volumeInMillimeters = length * depth * width;

//try to convert it back to meters
var volume = volumeInMillimeters / 1000;
alert(volumeInMillimeters);
alert(volume);

});
</script>

这里还有一个js小提琴,所以你可以看到它有效 - https://jsfiddle.net/xk4r8hm2/1/

Also here is a js fiddle so you can see it working - https://jsfiddle.net/xk4r8hm2/1/

如果有人可以请帮我这么做,不是只找个答案,而是要解释一下。

If someone can help me do this, not looking for just an answer but the explanation to go with it please.

谢谢!

推荐答案

你只需要将它除以1000三次即

You just need to divide it thrice by 1000 i.e.

var volume = volumeInMillimeters / (1000 * 1000 * 1000 );

这是因为卷有3个维度。如果你将每个维度乘以1000,那么要在 mm 中得到它,你需要有效地将每个维度除以1000,或者将最终结果除以(1000 * 1000) * 1000)。

This is because volume has 3 dimensions. If you have multiplied each dimension with 1000, then to get it in mm back, you need to effectively divide each dimension by 1000, or divide the final result by ( 1000 * 1000 * 1000 ).

这篇关于javascript音量计算器从宽度,长度和深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆