Purchase Appple Purchase Orange
Instructions
This assignment uses the logic from the recent quiz and assignment. You can see some of that code in there.
What is different? The sequence of operations, spending and finding money, is totally controllable with buttons.
This makes the program very flexible.
Update the Code below with the following:
- Add 2 new event functions: buyTortilla() and addFoundMoney().
- Add 2 new buttons that trigger buyTortilla() and addFoundMoney().
STARTER CODE
<!DOCTYPE html>
<html><head><style>p {font-family: arial; font-size: 20px;} </style></head>
<body>
<button onclick='buyApple()'>Purchase Appple</button>
<button onclick='buyOrange()'>Purchase Orange</button>
<!-- This marks space for a paragraph that we can update during the program -->
<p id="balance"></p>
<script>
// javascript code begin
// Step 1. initialize variables:
change=100 // starting balance
apple=10 // price of 1 apple
orange=20 // price of 1 orange
tortilla=30 // price of 1 tortilla
foundMoney=25 // pretend found $25
// initialize the user-interface when this page loads:
document.getElementById("balance").innerHTML = "$"+change;
// Step 2. define event functions for each button
function buyApple(){
change -= apple;
document.getElementById("balance").innerHTML = "$"+change;
}
function buyOrange(){
change -= orange;
document.getElementById("balance").innerHTML = "$"+change;
}
// Make 2 more functions here, to match the 2 new buttons:
// javascript code end
</script>
</body>
</html>
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images