2016-06-23 11 views
2

からボリューム電卓私は数学では驚くほどではないよ、私は認めなければならない:)はjavascriptの幅、長さ及び深さ

を基本的に私が取る電卓を作成する必要があります私は答えを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> 

あなたはそれが働いて見ることができるようにjsのフィドルある - https://jsfiddle.net/xk4r8hm2/1/

を誰かが私の手助けをすることができれば、答えを探すのではなく、それに付随する説明をお願いします。

ありがとうございます!

+1

あなたはjsfiddleのフロントページにリンクしました。 –

+0

ああ、私の悪い:)。今すぐ変更しました – virepo

答えて

4

あなただけのボリュームが3次元を持っているので、これがある

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

すなわち1000で三回、それを分割する必要があります。各ディメンションに1000を掛けた場合、 mmに戻すには、各ディメンションを1000で効果的に分割するか、最終結果を(1000 * 1000 * 1000)で除算する必要があります。

+0

ありがとう、これは動作します。私はなぜこれがうまくいくのか分かります。説明のためにありがとう! – virepo

+0

@virepoそれがあなたを助けてくれたことを喜んで:) –

関連する問題