Wednesday 17 May 2017

javascript radio button for loop, check box

chen assignment 3
asset value
depreciation rate (0-100)
years of depreciation
value after years

Choose pizza size
small $8
medium $10
large $15
extra large $18

Add specials
none
super cheesy $3
extra meaty $5
really veggy $2

Add extras
extra cheese $1.5
extra pepperoni $1.5
extra mushrooms $1.5
extra bacon $1.5
extra olives $1.5

name
address
phone number



total: $

//default.html


<!DOCTYPE html>
<html>
<head>
    <title>chen assignment 3</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="javascript.js">    
    </script>
</head>

<body>
    <table>
        <tr>
            <td>asset value</td>
            <td><input type="number" id="asset_value" /></td>
        </tr>
        <tr>
            <td>depreciation rate (0-100)</td>
            <td><input type="number" id="depreciation_rate" /></td>
        </tr>
        <tr>
            <td>years of depreciation</td>
            <td><input type="number" id="years" /></td>
        </tr>
        <tr>
            <td>value after years</td>
            <td><span id="value_after_years"></span></td>
        </tr>
        <tr>
            <td colspan="2"><input type="button" value="calculate value after depreciation" onclick="calculate()" /></td>
        </tr>
    </table>

    <hr />

    <form name="pizza_order">
        <b>Choose pizza size</b><br />
        <input type="radio" name="pizza_type" value="8" checked/>small $8<br/>
        <input type="radio" name="pizza_type" value="10" />medium $10<br />
        <input type="radio" name="pizza_type" value="15" />large $15<br />
        <input type="radio" name="pizza_type" value="18" />extra large $18<br />
        <br />
        <b>Add specials</b><br />
        <input type="radio" name="specials" value="0" checked/>none<br />
        <input type="radio" name="specials" value="3" />super cheesy $3<br />
        <input type="radio" name="specials" value="5" />extra meaty $5<br />
        <input type="radio" name="specials" value="2" />really veggy $2<br />
        <br />
        <b>Add extras</b><br />
        <input type="checkbox" name="extra_cheese" />extra cheese $1.5<br/>
        <input type="checkbox" name="extra_pepperoni" />extra pepperoni $1.5<br />
        <input type="checkbox" name="extra_mushrooms" />extra mushrooms $1.5<br />
        <input type="checkbox" name="extra_bacon" />extra bacon $1.5<br />
        <input type="checkbox" name="extra_olives" />extra olives $1.5<br />
        <br />
        <table>
            <tr>
                <td>name</td>
                <td><input type="text" /></td>
            </tr>
            <tr>
                <td>address</td>
                <td><input type="text" /></td>
            </tr>
            <tr>
                <td>phone number</td>
                <td><input type="text" /></td>
            </tr>
        </table>
        <br />
        <input type="button" value="place order" onclick="order();"/><br />
        <br />
        <b>total: $</b> <span id="total_cost"></span>
    </form>
</body>

</html>

//javascript.js


function calculate() {
    var asset_value = document.getElementById("asset_value").value;
    var depreciation_rate = document.getElementById("depreciation_rate").value;
    var years = document.getElementById("years").value;

    asset_value = parseFloat(asset_value);
    depreciation_rate = parseFloat(depreciation_rate) / 100;
    years = parseInt(years);
   

    for (var i = parseInt(1); i <= years; i++)
    {
        asset_value *= (1 - depreciation_rate);
    }

    document.getElementById("value_after_years").innerHTML = asset_value.toFixed(2);
}

function order() {
    var total = parseFloat(0);

    for (var i = parseInt(0); i < document.pizza_order.pizza_type.length; i++)
    {
        if (document.pizza_order.pizza_type[i].checked)
        {
            total += parseFloat(document.pizza_order.pizza_type[i].value);
        }
    }

    for (var i = parseInt(0); i < document.pizza_order.specials.length; i++)
    {
        if (document.pizza_order.specials[i].checked) {
            total += parseFloat(document.pizza_order.specials[i].value);
        }
    }

    if (document.pizza_order.extra_cheese.checked)
    {
        total += 1.5;
    }

    if (document.pizza_order.extra_pepperoni.checked) {
        total += 1.5;
    }

    if (document.pizza_order.extra_mushrooms.checked) {
        total += 1.5;
    }

    if (document.pizza_order.extra_bacon.checked) {
        total += 1.5;
    }

    if (document.pizza_order.extra_olives.checked) {
        total += 1.5;
    }

    document.getElementById("total_cost").innerHTML = total;
}

No comments:

Post a Comment