Je ne suis pas étonnant en maths, je dois admettre :)javascript calculatrice volume à partir de la largeur, la longueur et la profondeur
Essentiellement, je dois créer une calculatrice qui prend:
1. width
2. length
3. depth
A partir de ces entrées, Je dois afficher la réponse en m³.
Faire un peu plus délicat, un utilisateur peut choisir entre 5 options déroulantes:
1. Centimeter
2. Inches
3. Feett
4. Yards
5. Meters
Par exemple, un utilisateur peut faire quelque chose comme:
width = 10 centimeters
length = 7 foot
depth = 2 inches
Alors ma pensée était convertir toutes les entrées de l'utilisateur en millimètres pour les rendre du même type. La formule pour trouver un volume est:
length * height * width
Je figure si je le fais en millimètres, je puis reconvertir en mètres. Cependant, je rencontre des problèmes et ne pas obtenir les bonnes réponses de mon code. La logique doit être complètement faux (Comme je l'ai dit mes mathématiques ne sont pas étonnant)
Voici mon 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>
est un violon js vous pouvez donc ici aussi voir travailler - https://jsfiddle.net/xk4r8hm2/1/
Si quelqu'un peut m'aider à faire cela, je ne cherche pas seulement une réponse, mais l'explication pour aller avec s'il vous plaît.
Merci!
Vous avez lié à la page d'accueil de jsfiddle. –
aha, ma mauvaise :). Changé maintenant – virepo