javascript volume calculator from width, length and depth

邮差的信 提交于 2019-12-24 01:41:15

问题


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

Essentially I need to create a calculator that takes:

1. width
2. length
3. depth

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

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 )

Here is my code:

    <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>

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.

Thanks!


回答1:


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

var volume = volumeInMillimeters / (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 ).



来源:https://stackoverflow.com/questions/37993923/javascript-volume-calculator-from-width-length-and-depth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!