JSFiddle - React, Tailwind, and code Playground
JavaScript
var books = [
{
id: "easyreader",
name: "easy reader",
price: 4.95,
discount: 0.10},
{
id: "diaryofwimpykid",
name: "diary of a wimpy kid",
price: 6.95,
discount: 0.10},
{
id: "lucid",
name: "lucid",
price: 14.95,
discount: 0.15},
{
id: "frederickdouglas",
name: "frederick douglas",
price: 25.95,
discount: 0.20}
];
function OrderBook() {
var total = 0;
bookname = prompt("Enter the name of the book that you want");
qty = prompt("Enter the no. of books that you want");
for (i = 0; i < books.length; i++) {
if (bookname == books[i].name) {
cost_of_order = (qty * books[i].price);
totaldiscountprice = (qty * books[i].price) * books[i].discount;
total = (qty * books[i].price) - totaldiscountprice;
document.write("The cost of buying <b>" + qty + "</b> of <b>" + books[i].name + "</b> is <b>$" + cost_of_order.toFixed(2) + "</b><br/>");
document.write("The discount for this purchase is <b>$" + totaldiscountprice.toFixed(2) + "</b><br/>");
document.write("With discount, your total order cost is <b>$" + total.toFixed(2) + "</b><br/>");
break;
}
}
if (i == books.length) {
alert("Sorry, " + user_name + " You entered an invalid product. We will not include this in your order.");
}
if (confirm("Would you like to order another book?")) {
total += OrderBook();
}
return total;
}
var user_name = "";
user_name = prompt("Welcome to Children's Nook Books. Please enter your name ", " ");
var sentence = "Hello " + user_name + " Please look through our available products and services before placing your order.";
alert(sentence);
var total_cost = 0;
total_cost += OrderBook();
document.write("<br/>With discount, your grand total is <b>$" + total_cost.toFixed(2) + "</b>");