-5

如何使用相同或更好的算法将此 PHP 函数转换为 JavaScript?

error_reporting( E_ALL ^ E_NOTICE );
if($_GET['text008']=="1br")
{   $typeservice=119;
    if(isset($_GET['extras']))
    foreach ($_GET['extras'] as $k){   
    $total +=$k;
    } 
    echo "Service: $" . $typeservice . "<br/>" . "Extra Fees: $" .$total . "<br/>";

    echo "Service w/ Extra Fee: $" . $balyo=$typeservice + $total;

    if(isset($_GET['freq']))
    {
        if($_GET['freq']=="O")
        {
            echo "<br/>Once";
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;     
        }
        if($_GET['freq']=="M")
        {   
            echo "<br/>Monthly";    
            $lahat = $balyo - ($balyo * .10);
            echo "<br/>Discount:$ " . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }       
        if($_GET['freq']=="BW")
        {
            echo "<br/>Bi-Weekly";  
            $lahat = $balyo - ($balyo * .15);
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }
        if($_GET['freq']=="W")
        {
            echo "<br/>Weekly"; 
            $lahat = $balyo - ($balyo * .15);
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }       
    }

}



if($_GET['text008']=="2br")
{
    $typeservice=139;
    if(isset($_GET['extras']))
    foreach ($_GET['extras'] as $k){   
    $total +=$k;
    } 
    echo "Service: $" . $typeservice . "<br/>" . "Extra Fees: $" .$total . "<br/>";

    echo "Service w/ Extra Fee: $" . $balyo=$typeservice + $total;

    if(isset($_GET['freq']))
    {
        if($_GET['freq']=="O")
        {
            echo "<br/>Once";
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;     
        }
        if($_GET['freq']=="M")
        {   
            echo "<br/>Monthly";    
            $lahat = $balyo - ($balyo * .10);
            echo "<br/>Discount:$ " . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }       
        if($_GET['freq']=="BW")
        {
            echo "<br/>Bi-Weekly";  
            $lahat = $balyo - ($balyo * .15);
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }
        if($_GET['freq']=="W")
        {
            echo "<br/>Weekly"; 
            $lahat = $balyo - ($balyo * .15);
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }       
    }

}
if($_GET['text008']=="3br")
{
    $typeservice=159;
    if(isset($_GET['extras']))
    foreach ($_GET['extras'] as $k){   
    $total +=$k;
    } 
    echo "Service: $" . $typeservice . "<br/>" . "Extra Fees: $" .$total . "<br/>";

    echo "Service w/ Extra Fee: $" . $balyo=$typeservice + $total;

    if(isset($_GET['freq']))
    {
        if($_GET['freq']=="O")
        {
            echo "<br/>Once";
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;     
        }
        if($_GET['freq']=="M")
        {   
            echo "<br/>Monthly";    
            $lahat = $balyo - ($balyo * .10);
            echo "<br/>Discount:$ " . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }       
        if($_GET['freq']=="BW")
        {
            echo "<br/>Bi-Weekly";  
            $lahat = $balyo - ($balyo * .15);
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }
        if($_GET['freq']=="W")
        {
            echo "<br/>Weekly"; 
            $lahat = $balyo - ($balyo * .15);
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }       
    }

}
if($_GET['text008']=="4br")
{
    $typeservice=189;
    if(isset($_GET['extras']))
    foreach ($_GET['extras'] as $k){   
    $total +=$k;
    } 
    echo "Service: $" . $typeservice . "<br/>" . "Extra Fees: $" .$total . "<br/>";

    echo "Service w/ Extra Fee: $" . $balyo=$typeservice + $total;

    if(isset($_GET['freq']))
    {
        if($_GET['freq']=="O")
        {
            echo "<br/>Once";
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;     
        }
        if($_GET['freq']=="M")
        {   
            echo "<br/>Monthly";    
            $lahat = $balyo - ($balyo * .10);
            echo "<br/>Discount:$ " . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }       
        if($_GET['freq']=="BW")
        {
            echo "<br/>Bi-Weekly";  
            $lahat = $balyo - ($balyo * .15);
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }
        if($_GET['freq']=="W")
        {
            echo "<br/>Weekly"; 
            $lahat = $balyo - ($balyo * .15);
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }       
    }

}
if($_GET['text008']=="5br")
{
    $typeservice=219;
        if(isset($_GET['extras']))
    foreach ($_GET['extras'] as $k){   
    $total +=$k;
    } 
    echo "Service: $" . $typeservice . "<br/>" . "Extra Fees: $" .$total . "<br/>";

    echo "Service w/ Extra Fee: $" . $balyo=$typeservice + $total;

    if(isset($_GET['freq']))
    {
        if($_GET['freq']=="O")
        {
            echo "<br/>Once";
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;     
        }
        if($_GET['freq']=="M")
        {   
            echo "<br/>Monthly";    
            $lahat = $balyo - ($balyo * .10);
            echo "<br/>Discount:$ " . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }       
        if($_GET['freq']=="BW")
        {
            echo "<br/>Bi-Weekly";  
            $lahat = $balyo - ($balyo * .15);
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }
        if($_GET['freq']=="W")
        {
            echo "<br/>Weekly"; 
            $lahat = $balyo - ($balyo * .15);
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }       
    }

}
if($_GET['text008']=="6br")
{
    $typeservice=249;
    if(isset($_GET['extras']))
    foreach ($_GET['extras'] as $k){   
    $total +=$k;
    } 
    echo "Service: $" . $typeservice . "<br/>" . "Extra Fees: $" .$total . "<br/>";

    echo "Service w/ Extra Fee: $" . $balyo=$typeservice + $total;

    if(isset($_GET['freq']))
    {
        if($_GET['freq']=="O")
        {
            echo "<br/>Once";
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;     
        }
        if($_GET['freq']=="M")
        {   
            echo "<br/>Monthly";    
            $lahat = $balyo - ($balyo * .10);
            echo "<br/>Discount:$ " . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }       
        if($_GET['freq']=="BW")
        {
            echo "<br/>Bi-Weekly";  
            $lahat = $balyo - ($balyo * .15);
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }
        if($_GET['freq']=="W")
        {
            echo "<br/>Weekly"; 
            $lahat = $balyo - ($balyo * .15);
            echo "<br/>Discount: $" . ($balyo * .15);
            echo "<br/> Total: $" . $lahat;             
        }       
    }
}
4

1 回答 1

0

我在 Facebook 上回复了更新的jsFiddle,但我想我会在这里发帖以防万一。这是一个 jsFiddle 设置,加上 if 语句中缺少花括号,并将金额设置为整数而不是字符串。

function CalculateOrder(form)
{   
    //sample lang to sir ng javascript pero ito mismo na yung code.
    //naka comment sa html ung code sa php 
    if(form.text008.value == "1br")
    {
        form.amount.value ="119.00";
    }

    if(form.text008.value == "1br" && form.extrass.checked ==true)
    {
        form.amount.value = "139";
    }
}
于 2013-07-22T20:13:13.870 回答