Last active
May 12, 2022 21:07
-
-
Save maxmatthews/4fbcdf8e37e5d95d752b6ec9986c0fd7 to your computer and use it in GitHub Desktop.
Browser Tip Calculator
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
document.getElementById("calculateButton").onclick = () => { | |
const totalInput = document.getElementById("total"); | |
const tipInput = document.getElementById("tip"); | |
console.log(typeof totalInput.value); | |
console.log(typeof tipInput.value); | |
const total = parseFloat(totalInput.value); | |
const tip = parseInt(tipInput.value); | |
const tipAmount = total * (tip / 100); | |
document.getElementById("results").innerHTML = | |
"Your tip should be $" + | |
numberRounder(tipAmount) + | |
". For a total of $" + | |
numberRounder(total + tipAmount) + | |
"."; | |
}; | |
const numberRounder = (input) => { | |
return Math.round(input * 100) / 100; | |
}; |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta | |
name="viewport" | |
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" | |
/> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge" /> | |
<title>Tip Calculator</title> | |
</head> | |
<body> | |
<label>Bill Total ($):</label> | |
<input type="number" id="total" /> | |
<br /> | |
<label>Tip Amount (%):</label> | |
<input type="number" id="tip" /> | |
<br /> | |
<button id="calculateButton">Calculate</button> | |
<div id="results"></div> | |
<script src="tip-script.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment