CS 170 Computer Applications For Business Fall 2016 Assignme ✓ Solved
CS 170 Computer Applications For Businessfall 2016 Assig
Problem to solve: The Serendipity Booksellers has a book club that awards points to its customers based on the number of books purchased each month.
The points are awarded as follows:
- If a customer purchases 0 books, they earn 0 points
- If a customer purchases 1 book, they earn 4 points
- If a customer purchases 2 books, they earn 8 points
- If a customer purchases 3 books, they earn 16 points
- If a customer purchases more than 3 books, they earn an additional 7 points on top of the 16 points for each book above 3.
Serendipity Booksellers also awards points based on the dollar value spent on non-book merchandise. Four (4) points are awarded for every $10 spent. Partial awards are not provided. For example, 4 points will be awarded for spending $10.00 up to $10.99. No points are awarded for less than $10.
Preferred Customers receive a bonus of double award points. The Serendipity Booksellers website needs to be updated to ask the customer to enter the number of books purchased last month, the dollars spent on non-book merchandise, confirm if they are a Preferred Customer, and then calculate and display the number of award points earned.
Requirements:
- Your program will calculate the award points as described above.
- You will generate HTML comments to add your name, section and TA name.
- Utilize a
<form>tag. - You will then add the HTML tags with the proper JavaScript code to produce:
- A textbox to solicit the number of books purchased.
- A textbox to solicit the dollars of non-book merchandise.
- Grouped radio buttons to determine Preferred Customer membership.
- A button to calculate the number of points awarded.
- A readonly textbox to display the result of the calculation.
- Utilize JavaScript comments to explain the steps you are performing within your code.
- Utilize at least one if/else statement. Consider using the if/else in determining the bonus points.
Additional Information: Make sure to convert input to numbers using parseInt() to avoid issues with calculations. Utilize Math.floor() for rounding down to the nearest integer.
Paper For Above Instructions
The following code implements the requirements set forth in the assignment for Serendipity Booksellers. It allows customers to calculate their reward points based on the number of books purchased and the amount spent on non-book merchandise, incorporating all necessary functionalities such as input validation, event handling, and point calculation logic.
HTML Structure
The HTML code includes a form structured to collect user input for the required fields. This includes:
- A textbox for the number of books purchased.
- A textbox for the dollars spent on non-book merchandise.
- Radio buttons to indicate if the customer is a Preferred Customer.
- A button for submission that triggers point calculation.
- A readonly textbox to display the awarded points.
HTML Example:
<!DOCTYPE html>
<html>
<head>
<!-- Author: [Your Name] -->
<!-- Section: [Your Section] -->
<!-- TA Name: [Your TA's Name] -->
<title>Serendipity Booksellers Reward Points Calculator</title>
</head>
<body>
<h1>Serendipity Booksellers Reward Points</h1>
<form id="pointsForm">
<p>Number of Books Purchased:
<input type="number" id="booksPurchased" min="0" required>
</p>
<p>Dollars Spent on Non-Book Merchandise:
<input type="number" id="nonBookSpent" min="0 required>
</p>
<p>Preferred Customer?
<input type="radio" name="preferred" value="yes">Yes
<input type="radio" name="preferred" value="no">No
</p>
<p>
<button type="button" onclick="calculatePoints()">Calculate Points</button>
</p>
<p>Points Awarded:
<input type="text" id="pointsAwarded" readonly>
</p>
</form>
<script>
function calculatePoints() {
// Declaring variables
var books = parseInt(document.getElementById('booksPurchased').value);
var nonBook = parseInt(document.getElementById('nonBookSpent').value);
var preferred = document.querySelector('input[name="preferred"]:checked');
var points = 0;
// Calculate points based on books purchased
if (books === 0) {
points = 0;
} else if (books === 1) {
points = 4;
} else if (books === 2) {
points = 8;
} else if (books === 3) {
points = 16;
} else {
points = 16 + (books - 3) * 7;
}
// Calculate points based on non-book spending
if (nonBook >= 10) {
points += Math.floor(nonBook / 10) * 4;
}
// Double points for preferred customers
if (preferred && preferred.value === "yes") {
points *= 2;
}
// Display total points awarded
document.getElementById('pointsAwarded').value = points;
}
</script>
</body>
</html>
Conclusion
This implementation meets the requirements laid out in the assignment by effectively capturing user inputs, calculating points using JavaScript logic, and providing immediate feedback within the browser. The comments within the code clarify the function of each segment, adhering to best practices for readability and maintainability.
References
- Fluency 6 - Chapter 17 - Fundamental Concepts
- Expressed in JavaScript - Chapter 18 – A JavaScript Program
- w3schools.com - JavaScript Documentation
- Mozilla Developer Network - JavaScript Guide
- JavaScript.info - The Modern JavaScript Tutorial
- Codecademy - Learn JavaScript Course
- FreeCodeCamp - JavaScript Algorithms and Data Structures
- MDN Web Docs - Understanding JavaScript Hoisting
- JavaScript: The Definitive Guide by David Flanagan
- JavaScript Patterns by Stoyan Stefanov