-
-
Notifications
You must be signed in to change notification settings - Fork 116
chore: tip calculator coding challenge prototype #719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Tip Calculator</title> | ||
<link rel="stylesheet" href="styles.css"> | ||
</head> | ||
|
||
<body> | ||
|
||
<div class="container"> | ||
<h1>Tip Calculator</h1> | ||
<div class="result"> | ||
<p><strong>Tip:</strong> <span id="tipAmount">$0.00</span></p> | ||
<p><strong>Tip Per Person:</strong> <span id="tipPerPerson">$0.00</span></p> | ||
<p><strong>Total Amount:</strong> <span id="totalAmount">$0.00</span></p> | ||
<p><strong>Amount Per Person:</strong> <span id="amountPerPerson">$0.00</span></p> | ||
</div> | ||
|
||
<div class="calculator"> | ||
|
||
<form id="tipCalculatorForm"> | ||
<div class="form-group"> | ||
<label for="price">Price</label> | ||
<input type="number" id="price" name="price" placeholder="Enter amount" value="25"> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<label for="tipPercentage">Tip %</label> | ||
<input type="number" id="tipPercentage" name="tipPercentage" placeholder="Enter tip percentage" | ||
value="15"> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<label for="numPeople">People Sharing</label> | ||
<input type="number" id="numPeople" name="numPeople" placeholder="Enter number of people" value="1"> | ||
</div> | ||
|
||
<button type="submit" id="calculateBtn">Calculate</button> | ||
<button type="reset" id="clearBtn">Clear</button> | ||
</form> | ||
|
||
</div> | ||
</div> | ||
|
||
<script src="script.js"></script> | ||
</body> | ||
|
||
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
document.addEventListener("DOMContentLoaded", function () { | ||
|
||
const priceInput = document.getElementById("price"); | ||
const tipPercentageInput = document.getElementById("tipPercentage"); | ||
const numPeopleInput = document.getElementById("numPeople"); | ||
|
||
const tipAmountDisplay = document.getElementById("tipAmount"); | ||
const tipPerPersonDisplay = document.getElementById("tipPerPerson"); | ||
const totalAmountDisplay = document.getElementById("totalAmount"); | ||
const amountPerPersonDisplay = document.getElementById("amountPerPerson"); | ||
|
||
const calculateButton = document.getElementById("calculateBtn"); | ||
const clearButton = document.getElementById("clearBtn"); | ||
|
||
calculateButton.addEventListener("click", function (event) { | ||
event.preventDefault(); | ||
calculateTip(); | ||
}); | ||
|
||
clearButton.addEventListener("click", function (event) { | ||
event.preventDefault(); | ||
clearFields(); | ||
}); | ||
|
||
function calculateTip() { | ||
let price = parseFloat(priceInput.value); | ||
let tipPercentage = parseFloat(tipPercentageInput.value); | ||
let numPeople = parseInt(numPeopleInput.value); | ||
|
||
if (isNaN(price) || isNaN(tipPercentage) || isNaN(numPeople) || numPeople <= 0) { | ||
alert("Please enter valid values."); | ||
return; | ||
} | ||
|
||
let totalTip = (price * (tipPercentage / 100)).toFixed(2); | ||
let tipPerPerson = (totalTip / numPeople).toFixed(2); | ||
let totalAmount = (price + parseFloat(totalTip)).toFixed(2); | ||
let amountPerPerson = (totalAmount / numPeople).toFixed(2); | ||
|
||
tipAmountDisplay.innerText = `$${totalTip}`; | ||
tipPerPersonDisplay.innerText = `$${tipPerPerson}`; | ||
totalAmountDisplay.innerText = `$${totalAmount}`; | ||
amountPerPersonDisplay.innerText = `$${amountPerPerson}`; | ||
} | ||
|
||
function clearFields() { | ||
priceInput.value = ""; | ||
tipPercentageInput.value = ""; | ||
numPeopleInput.value = "1"; | ||
|
||
tipAmountDisplay.innerText = "$0.00"; | ||
tipPerPersonDisplay.innerText = "$0.00"; | ||
totalAmountDisplay.innerText = "$0.00"; | ||
amountPerPersonDisplay.innerText = "$0.00"; | ||
} | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
body { | ||
font-family: Arial, sans-serif; | ||
background-color: #d5d1d1; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 100vh; | ||
margin: 0; | ||
} | ||
|
||
.container { | ||
background: white; | ||
padding: 20px; | ||
border-radius: 10px; | ||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); | ||
width: 320px; | ||
} | ||
|
||
h1 { | ||
background: #1e1a2e; | ||
color: white; | ||
padding: 10px; | ||
border-radius: 5px; | ||
margin-bottom: 20px; | ||
text-align: center; | ||
font-size: 1.5rem; | ||
} | ||
|
||
.result { | ||
background: #e0e5d7; | ||
padding: 10px; | ||
margin-bottom: 15px; | ||
border-radius: 5px; | ||
} | ||
|
||
.result p { | ||
margin: 5px 0; | ||
font-size: 1rem; | ||
} | ||
|
||
.calculator { | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
.form-group { | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
margin-bottom: 10px; | ||
} | ||
|
||
label { | ||
font-weight: bold; | ||
font-size: 1rem; | ||
flex: 1; | ||
margin-right: 10px; | ||
} | ||
|
||
input { | ||
padding: 8px; | ||
border: 1px solid #ccc; | ||
border-radius: 5px; | ||
font-size: 1rem; | ||
flex: 2; | ||
} | ||
|
||
button { | ||
padding: 10px; | ||
margin-top: 10px; | ||
border: none; | ||
border-radius: 5px; | ||
cursor: pointer; | ||
font-size: 1rem; | ||
font-weight: bold; | ||
width: 100%; | ||
} | ||
|
||
button:first-of-type { | ||
background: #1e1a2e; | ||
color: white; | ||
} | ||
|
||
button:last-of-type { | ||
background: #a59898; | ||
color: black; | ||
} | ||
|
||
button:hover { | ||
opacity: 0.9; | ||
} | ||
|
||
@media (max-width: 480px) { | ||
.container { | ||
width: 90%; | ||
padding: 15px; | ||
} | ||
|
||
h1 { | ||
font-size: 1.25rem; | ||
} | ||
|
||
input, button { | ||
font-size: 0.9rem; | ||
} | ||
|
||
.form-group { | ||
flex-direction: column; | ||
align-items: flex-start; | ||
} | ||
|
||
label { | ||
margin-bottom: 5px; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Fulfill the below user stories. Feel free to give it your own personal style. | ||
|
||
1. As a user, I should be able to enter a bill amount, tip percentage, and the number of people sharing the bill so that I can calculate the total cost per person. | ||
|
||
1. As a user, I should be able to click a "Calculate" button to compute the tip and total cost so that I can get the results. | ||
|
||
1. As a user, I should be able to to see the calculated tip amount, tip per person, total amount including the tip, and the total amount per person so that I can understand the cost breakdown. | ||
|
||
1. As a user, I should be able to to reset all input fields and calculated values by clicking a "Clear" button so that I can start a new calculation easily. | ||
|
||
1. As a user, I should see the results calculated to 2 decimal places. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.