从宽度、长度和深度计算javascript体积计算器
我必须承认,我的数学不太好:) 基本上,我需要创建一个计算器,它需要:从宽度、长度和深度计算javascript体积计算器,javascript,jquery,math,calculator,volume,Javascript,Jquery,Math,Calculator,Volume,我必须承认,我的数学不太好:) 基本上,我需要创建一个计算器,它需要: 1. width 2. length 3. depth 根据这些输入,我需要以m³为单位显示答案 用户可以在5个下拉选项中进行选择,这会让操作变得更加复杂: 1. Centimeter 2. Inches 3. Feett 4. Yards 5. Meters 例如,用户可以执行以下操作: width = 10 centimeters length = 7 foot depth = 2 inches 所以我的想法是将所
1. width
2. length
3. depth
根据这些输入,我需要以m³为单位显示答案
用户可以在5个下拉选项中进行选择,这会让操作变得更加复杂:
1. Centimeter
2. Inches
3. Feett
4. Yards
5. Meters
例如,用户可以执行以下操作:
width = 10 centimeters
length = 7 foot
depth = 2 inches
所以我的想法是将所有用户输入转换成毫米,使它们成为相同的类型。求体积的公式为:
length * height * width
所以我想如果我以毫米为单位,我可以把它转换回米。但是,我遇到了问题,没有从代码中得到正确的答案。逻辑一定是完全错误的(就像我说我的数学不惊人)
这是我的密码:
<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>
宽度
厘米
英寸
脚
码
米
长度
厘米
英寸
脚
码
米
深度
厘米
英寸
脚
码
米
算计
$(“#计算”)。单击(函数(){
//这些是每种下拉菜单类型中的毫米数
var数组=新数组();
数组['cm']='10';
数组['inches']='25.4';
数组['feet']='304.8';
数组['yards']='914.4';
数组['meters']='1000';
//以毫米为单位查找宽度
var widthVal=$('#width').val();
var widthType=$('#width type').val();
var width=widthVal*数组[widthType];
//以毫米为单位计算长度
var lengthVal=$('#length').val();
var lengthType=$('#length type').val();
变量长度=长度值*数组[长度类型];
//以毫米为单位计算深度
var depthVal=$('#depth').val();
var depthType=$(“#深度类型”).val();
var depth=depthVal*数组[depthType];
//以毫米为单位计算总体积
var volumeInMillimeters=长度*深度*宽度;
//尝试将其转换回米
var体积=体积毫米/1000;
警报(体积毫米);
警报(音量);
});
这里还有一把js小提琴,你可以看到它在工作-
如果有人能帮我做到这一点,请不要只是寻找答案,而是解释
谢谢 只需将其三次除以1000即可
var volume = volumeInMillimeters / (1000 * 1000 * 1000 );
这是因为体积有3个维度。如果将每个维度乘以1000,那么要将其返回
mm
中,需要有效地将每个维度除以1000,或者将最终结果除以(1000*1000*1000)。您已经链接到了JSFIDLE的frontpage.aha,我的坏:)。现在就改了谢谢你,这很有效。我确实明白为什么这会奏效,所以谢谢你的解释@virepo很高兴知道它帮助了您:)