mercredi 2 juin 2021

if/else statement didn't work inside function

Hi I just writing a simple code to calculate the cheapest price by unit of eggs depend on the quantity coming in the box(12pcs,18pcs or 20pcs)

// var quantity of eggs//
var docena="12";
var dieciocho="18";
var veinte="20";
//type price by quantity//
var precio_docena=prompt("dozen");
var precio_dieciocho=prompt("eighteen");
var precio_veinte=prompt("twenty");
//calculate price by unit//
var precio_unidad12=precio_docena/docena;
var precio_unidad18=precio_dieciocho/dieciocho;
var precio_unidad20=precio_veinte/veinte;

//shows cheaper//    

if
(precio_unidad12<precio_unidad18 && precio_unidad12<precio_unidad20){
    document.getElementById("minimun").innerHTML="dozen";
}
else if
(precio_unidad12>precio_unidad18 && precio_unidad12<precio_unidad20){
    document.getElementById("med").innerHTML="eighteen";
}
else 
{
    document.getElementById("highest").innerHTML="twenty";
}

when i run that works like i wanted but if i enclose the if else statement into a function just show me the else statement only (just shows twenty not matter who statement is true)

//var quantities of eggs//
var docena="12";
var dieciocho="18";
var veinte="20";
//type price by quantity//
var precio_docena=prompt("dozen");
var precio_dieciocho=prompt("eighteen");
var precio_veinte=prompt("twenty");
//calculate price by unit//
var precio_unidad12=precio_docena/docena;
var precio_unidad18=precio_dieciocho/dieciocho;
var precio_unidad20=precio_veinte/veinte;

 //shows cheaper//   
function calculate(){
if
(precio_unidad12<precio_unidad18 && precio_unidad12<precio_unidad20){
    document.getElementById("minimun").innerHTML="dozen";
}
else if
(precio_unidad12>precio_unidad18 && precio_unidad12<precio_unidad20){
    document.getElementById("med").innerHTML="eighteen";
}
else 
{
    document.getElementById("highest").innerHTML="twenty";
}
}
calculate();


what can i fix to this work inside of a function i need to call the function in the future thanks and sorry English its my second language

Aucun commentaire:

Enregistrer un commentaire