javascript音量计算器从宽度,长度和深度

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

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

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 

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

这是我的代码:

   Width    Centimeter Inches Feet Yards Meters     Length    Centimeter Inches Feet Yards Meters     Depth    Centimeter Inches Feet Yards Meters          $('#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); });  

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

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

谢谢!

你需要将它分成三次,即1000

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

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