'Calculate button
Good day everyone
I am doing a code that is basically a small shopping cart. I need to add a calculate button. But I am not sure what I am doing wrong. Either the program disappears completely or the button just doesn't work.
It is supposed to calculate the total price of all 4 items in the basket. There is an add and minus button that adds and subtract the quality of your items. The total button should look at the total and multiply it with the price then add it up together.
The code I have at the moment at least keep the program.
var nums = [1, 2, 3, 4];
var items = ["Coke", "Kit Kat", "Bar One", "Fanta"];
var prices = [7.5, 9.5, 8.5, 7.5];
var quantities = [0, 0, 0, 0];
var totals = [0.0, 0.0, 0.0, 0.0];
var totalOrderAmt = 0;
var calculate = 0;
function calculate(totalOrderAmt) {
document.getElementById("button");
totalOrderAmt = totals * prices;
alert(totalOrderAmt);
}
function add_selection(x) {
console.log(x);
quantities[x] = quantities[x] + 1;
totals[x] = prices[x] * quantities[x];
totalOrderAmt += prices[x];
display_all();
}
function minus_selection(x) {
if (quantities[x] <= 0) {
display_all();
return alert("Add before Minus!")
} else if (quantities[x] > 0) {
console.log(x);
quantities[x] = quantities[x] - 1;
totals[x] = prices[x] * quantities[x];
totalOrderAmt -= prices[x];
display_all();
}
}
function displayTotal() {
document.getElementById("total").innerHTML += "<br/><br/>Total order amount is R" + totalOrderAmt.toFixed(2);
}
function display_all() {
var myTable = "<table><th style='width: 100px; color: red; text-align: right;'>Num</th>";
myTable += "<th style='width: 100px; color: red; text-align: right;'>Item</th>";
myTable += "<th style='width: 100px; color: red; text-align: right;'>Price</th>";
myTable += "<th style='width: 100px; color: red; text-align: right;'>Quantity</th>";
myTable += "<th style='width: 100px; color: red; text-align: right;'>Total</th>";
myTable += "<th style='width: 100px; color: red; text-align: right;'>Add</th>";
myTable += "<th style='width: 100px; color: red; text-align: right;'>Minus</th>";
for (i = 0; i < items.length; i++) {
myTable += "<tr><td style='width: 100px; text-align: right;'>" + nums[i] + "</td>";
myTable += "<td style='width: 100px; text-align: right;'>" + items[i] + "</td><";
myTable += "<td style='width: 100px; text-align: right;'>" + prices[i] + "</td>";
myTable += "<td style='width: 100px; text-align: right;'>" + quantities[i] + "</td>";
myTable += "<td style='width: 100px; text-align: right;'>" + totals[i] + "</td>";
myTable += "<td><button onclick='add_selection(" + i + ")'>Add</button></td>";
myTable += "<td><button onclick='minus_selection(" + i + ")'>Minus</button></td>";
}
myTable += "</table>";
// myTable += "<br/><br/><p>Total: " + totalOrderAmt + "</p>";
// document.write(myTable);
document.getElementById("demo").innerHTML = myTable;
}
window.onload = function() {
display_all();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
