In: Computer Science
Based on the descriptions given below, write the JavaScript codes to calculate the amount to
be paid for books purchased.
Declare all the variables used in this program.
Ask the user to key-in the book code using a prompt() method. Store the value in a
variable named book_code.
Ask the user to key-in the number of books purchased using a prompt() method.
Store the value in a variable named book_qty.
Ask the user whether they have a membership card of the bookstore using a
confirm() method. Store their response in a variable named member.
Now, using if else statement identify the book title and book price. Use the table
below as your reference.
Book Code
Book Title
Book Price (RM)
B001 or b001 E-Commerce 2019
112.00
B002 or b002 Learning Web Design
248.35
B003 or b003 Business Law
71.90
Other values
Invalid Book Code
0.00
Based on the user’s response on bookstore membership, use if else statement to
identify:
o If the user is a member, there will be a member discount of 20%. The
membership status will be “Yes”.
o If the user is not a member, there will not be any member discount given.
The membership status will be “No”.
Write the formula to calculate the total payment for the books purchased.
If total payment for the books purchased is more than RM500:
o The user is eligible to get additional discount of 30%.
o Use the alert() method to display amount of total purchase and inform users
about the discount eligibility.
o Otherwise, the user is not eligible to get the 30% discount. Use the alert()
method to display amount of total purchase and inform users about the
discount ineligibility.
Write the formula to calculate the total discount.
Write the formula to calculate the discount amount based on the total purchase
amount.
Write the formula to calculate the payment after deduction of discount.
Display the output as shown in the sample output sequence.
Here is your code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>BookStore</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.css">
</head>
<body>
<p id="demo"></p>
<header>
</header>
<script type="text/javascript">
var book_code = prompt("Please enter book code");
if(book_code=="B001"|| book_code=="b001"|| book_code=="B002"|| book_code=="b002"|| book_code=="B003"|| book_code=="b003"){
var book_qty = prompt("Please enter number of books purchased");
var member;
var r = confirm("Do you have a membership card of the bookstore?");
if (r == true) {
member = "Yes";
} else {
member = "No";
}
if (book_code=="B001"|| book_code=="b001") {
var book_title="E-Commerce 2019";
var book_price=112
var tbook_price=book_price*book_qty;
var total=memberdiscount(tbook_price);
if(total>500){
alert("You are eligible to get additional discount of 30% and the amount purchased is "+ total);
var adddis=total-total*(30/100);
}
else{
alert("You are not eligible to get additional discount of 30% and the amount purchased is "+ total);
var adddis=total;
}
// alert("Book Code="+book_code+"Book Title="+book_title+" Book Price (RM)="+book_price+" Total Qty="+book_qty+" Membership Status="+member+" Total amt="+adddis); You can also get out in alert format
}
if (book_code=="B002"|| book_code=="b002") {
var book_title="Learning Web Design";
var book_price=248.35
var tbook_price=book_price*book_qty;
var total=memberdiscount(tbook_price);
if(total>500){
alert("You are eligible to get additional discount of 30% and the amount purchased is "+ total);
var adddis=total-total*(30/100);
}
else{
alert("You are not eligible to get additional discount of 30% and the amount purchased is "+ total);
var adddis=total;
}
}
if (book_code=="B003"|| book_code=="b003") {
var book_title="Business Law";
var book_price=71.90
var tbook_price=book_price*book_qty;
var total=memberdiscount(tbook_price);
if(total>500){
alert("You are eligible to get additional discount of 30% and the amount purchased is "+ total);
var adddis=total-total*(30/100);
}
else{
alert("You are not eligible to get additional discount of 30% and the amount purchased is "+ total);
var adddis=total;
}
}
}
else{
alert("Invaild Book Code");
abort();
}
function memberdiscount(book_price) {
if(member=="Yes")
{
var booktotal=book_price*(20/100);
return book_price-booktotal;
}
else{
var booktotal=0;
return book_price-booktotal;
}
}
document.getElementById("demo").innerHTML ="<b>Purchased Details:</b><br>"+ "Book Code="+book_code+"<br>Book Title="+book_title+"<br>Book Price (RM)="+book_price+"<br>Total Qty="+book_qty+"<br>Membership Status="+member+"<br>Total amt to pay:="+adddis;
</script>
</body>
</html>
----------------------------------------
OUTPUT:
----------------------------------
Please rate and feel free to ask if you need any modifications. :)