Sie sind auf Seite 1von 1

AS SNACK MACHINE ANNOTATED

Array filled with 10 prices Variables Parameter stores the Array slot chosen
Array
filled
with 10
prices
Variables
Parameter stores the Array slot chosen

<html>

<head>

<script>

var price =new Array('Free',10,20,30,40,50,60,70,80,90); var chosen;

var ToPay var coin

function showPrice(chosen)

{

document.getElementById('txtPrice').value = price[chosen] ToPay = price[chosen] } function pay(coin) Place
document.getElementById('txtPrice').value = price[chosen]
ToPay = price[chosen]
}
function pay(coin)
Place Array slot into Textbox
{
coin will be
an integer of
either 1 or 5
!
!
if (ToPay>0)
!
!
{
If ToPay is not yet 0 then minus the coin
Integer sent to the function
!!!
!!! ToPay=ToPay-coin
document.getElementById('txtPrice').value = ToPay
! !
}
! !
else
Show the
! !
{
new ToPay
!!!
!!!
var change = 0-ToPay
alert('No more to Pay and you change is $0.0' + change)
!
!
}
}
</script>
If <=0 Work out and show change
</head>
<body bgcolor="FFFFAA">
<br>
<center>PICK A SNACK<br><br>
<input type='button' value='0' onClick='showPrice(0)'>
<input type='button' value='1' onClick='showPrice(1)'>
<input type='button' value='2' onClick='showPrice(2)'>
<input type='button' value='3' onClick='showPrice(3)'>
<input type='button' value='4' onClick='showPrice(4)'>
<input type='button' value='5' onClick='showPrice(5)'>
<input type='button' value='6' onClick='showPrice(6)'>
<input type='button' value='7' onClick='showPrice(7)'>
<input type='button' value='8' onClick='showPrice(8)'>
Each button sends the
Array slot required
showPrice(array slot)

<input type='button' value='9' onClick='showPrice(9)'><br><br><br> PRICE<br> <input type='text' id='txtPrice' size='5' align='right'><br><br> <input type='button' value='1 Cent' onClick='pay(1)'> <input type='button' value='5 Cents' onClick='pay(5)'> <input type='button' value='CHANGE' onClick='pay(0)'>

<br><br><br>

</center>

</body>

</html>

Each PAY button sends the CENTS as an integer showPrice(array slot)
Each PAY button sends the
CENTS as an integer
showPrice(array slot)