Midterm Part B

This commit is contained in:
2025-10-23 16:09:15 -04:00
parent d3d77666c9
commit b9e47e15f6
4 changed files with 268 additions and 0 deletions

View File

@@ -0,0 +1,130 @@
<!DOCTYPE html>
<html>
<head>
<title>Levi McLean Midterm Part B</title>
<meta charset="utf-8" lang="en-us">
</head>
<body>
<h1>Levi McLean Midterm Part B</h1>
<script>
const TAX_RATE = 0.13;
const DISCOUNT_RATE = 0.1;
var item1 = {
name: "apple",
price: 1.1,
qty: 4
};
var item2 = {
name: "banana",
price: 0.59,
qty: 2
};
var item3 = {
name: "pear",
price: 1.25,
qty: 6
};
var item4 = {
name: "grapes",
price: 2.25,
qty: 8
};
var item5 = {
name: "kiwi",
price: 3,
qty: 1
};
var cart = [item1, item2, item3, item4, item5];
function searchItem() {
var itemName = window.prompt("Enter an item name: ").toLowerCase();
var foundItem;
if (itemName == null) {
window.alert("Item name cannot be null.");
}
for (var i=0; i < cart.length; i++) {
if (cart[i].name === itemName) {
window.alert("Item(s) found.");
foundItem = cart[i];
return foundItem;
}
}
window.alert("Item(s) not found.");
return undefined;
}
function updateItemQuantity() {
var updateItem = window.prompt("Enter item name to update: ").toLowerCase();
if (updateItem == null) {
window.alert("Invalid item name.");
}
for (var i=0; i < cart.length; i++) {
if (cart[i].name === updateItem) {
var updateQty = parseInt(window.prompt("How many items do we add? "), 10);
cart[i].qty += updateQty;
console.log(cart);
return updateItem;
}
}
window.alert("Item does not exist in cart.");
return undefined;
}
function clearCart() {
for (var i = 0; i < cart.length; i++) {
cart[i].name = "";
cart[i].qty = 0;
}
console.log(cart);
window.alert("Cart has been cleared!");
}
function calulateFinalPriceWithDiscount() {
var subtotal = 0;
for (var i = 0; i < cart.length; i++) {
subtotal += cart[i].price * cart[i].qty;
}
var tax = subtotal * TAX_RATE;
var discount = subtotal * DISCOUNT_RATE;
var total = subtotal - discount + tax;
window.alert(
"Subtotal: $" + subtotal.toFixed(2) + "\n" +
"Tax: $" + tax.toFixed(2) + "\n" +
"Discount: $" + discount.toFixed(2) + "\n" +
"Total: $" + total.toFixed(2)
);
}
var notDone = true;
while(notDone === true) {
var userChoice = window.prompt("Please make a selection (s)earch, (u)pdate, (c)lear, (d)iscount: ").toLowerCase();
if (userChoice === "s") {
searchItem();
notDone = window.confirm("Do you wish to continue?");
} else if (userChoice === "u") {
updateItemQuantity();
notDone = window.confirm("Do you wish to continue?");
} else if (userChoice === "c") {
clearCart();
notDone = window.confirm("Do you wish to continue?");
} else if (userChoice === "d") {
calulateFinalPriceWithDiscount();
notDone = window.confirm("Do you wish to continue?");
} else {
window.alert("Invalid selection, try again.");
}
}
</script>
</body>
</html>

View File

@@ -0,0 +1,138 @@
<!DOCTYPE html>
<html>
<head>
<title>Levi McLean Midterm Part B</title>
<meta charset="utf-8" lang="en-us">
</head>
<body>
<h1>Levi McLean Midterm Part B</h1>
<script>
const TAX_RATE = 0.13;
const DISCOUNT_RATE = 0.1;
var item1 = {
name: "apple",
price: 1.1,
qty: 4
};
var item2 = {
name: "banana",
price: 0.59,
qty: 2
};
var item3 = {
name: "pear",
price: 1.25,
qty: 6
};
var item4 = {
name: "grapes",
price: 2.25,
qty: 8
};
var item5 = {
name: "kiwi",
price: 3,
qty: 1
};
var cart = [item1, item2, item3, item4, item5];
function searchItem() {
var itemName = window.prompt("Enter an item name: ").toLowerCase();
var foundItem;
if (itemName == null) {
window.alert("Item name cannot be null.");
}
for (var i=0; i <= cart.length; i++) {
if (cart[i].name === itemName) {
window.alert("Item(s) found.");
foundItem = cart[i];
return foundItem;
}
}
window.alert("Item(s) not found.");
return undefined;
}
function updateItemQuantity() {
var updateItem = window.prompt("Enter item name to update: ").toLowerCase();
if (updateItem == null) {
window.alert("Invalid item name.");
}
for (var i=0; i <= cart.length; i++) {
if (cart[i].name === updateItem) {
var updateQty = parseInt(window.prompt("How many items do we add? "), 10);
cart[i].qty += updateQty;
console.log(cart);
return updateItem;
}
window.alert("Item does not exist in cart.");
return undefined;
}
}
function clearCart() {
cart[0].name = "";
cart[0].qty = 0;
cart[1].name = "";
cart[1].qty = 0;
cart[2].name = "";
cart[2].qty = 0;
cart[3].name = "";
cart[3].qty = 0;
cart[4].name = "";
cart[4].qty = 0;
console.log(cart);
window.alert("Cart has been cleared!");
}
function calulateFinalPriceWithDiscount() {
var subtotal = 0;
var tax = 0;
var total = 0;
// for (var i = 0; i <= cart.length; i++) {
// subtotal += cart[i].price;
// }
subtotal += cart[0].price;
subtotal += cart[1].price;
subtotal += cart[2].price;
subtotal += cart[3].price;
subtotal += cart[4].price;
tax = subtotal * TAX_RATE;
discount = subtotal * DISCOUNT_RATE;
total = subtotal - discount + tax;
window.alert("Subtotal: $" + subtotal + " Tax: $" + tax + " Total: $" + total);
}
var notDone = true;
while(notDone === true) {
var userChoice = window.prompt("Please make a selection (s)earch, (u)pdate, (c)lear, (d)iscount: ").toLowerCase();
if (userChoice === "s") {
searchItem();
notDone = window.confirm("Do you wish to continue?");
} else if (userChoice === "u") {
updateItemQuantity();
notDone = window.confirm("Do you wish to continue?");
} else if (userChoice === "c") {
clearCart();
notDone = window.confirm("Do you wish to continue?");
} else if (userChoice === "d") {
calulateFinalPriceWithDiscount();
notDone = window.confirm("Do you wish to continue?");
} else {
window.alert("Invalid selection, try again.");
}
}
</script>
</body>
</html>