<!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 balanceapple=10 // price of 1 appleorange=20 // price of 1 orangetortilla=30 // price of 1 tortillafoundMoney=25 // pretend found $25// initialize the user-interface when this page loads:document.getElementById("balance").innerHTML = "$"+change;// Step 2. define event functions for each buttonfunction 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> Bug report: the previous algorithm allows people to buy things when they don't have enough money. Correct that bug: Perform some maintenance on your Button's program. Don't allow purchases when the change is less than the purchase. Provide some feedback when that happens. How: Using conditionals, prevent people from buying things when they don't have enough money. For each button function, add some type of notification in your if-statments when that happens such as You could give them a popup alert:alert("Denied: Insufficient balance"); Or write a message:document.write("Denied: Insufficient balance"); Or update a spot on the page (best method): document.getElementById("message").innerHTML= "Denied: Insufficient balance");
<!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>
Bug report: the previous
Correct that bug: Perform some maintenance on your Button's program.
- Don't allow purchases when the change is less than the purchase.
- Provide some feedback when that happens.
How:
- Using conditionals, prevent people from buying things when they don't have enough money.
- For each button function, add some type of notification in your if-statments when that happens such as
- You could give them a popup alert:alert("Denied: Insufficient balance");
-
- Or write a message:document.write("Denied: Insufficient balance");
- Or update a spot on the page (best method): document.getElementById("message").innerHTML= "Denied: Insufficient balance");
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images