Skip to content

Instantly share code, notes, and snippets.

@maxmatthews
Last active May 12, 2022 21:07
Show Gist options
  • Save maxmatthews/4fbcdf8e37e5d95d752b6ec9986c0fd7 to your computer and use it in GitHub Desktop.
Save maxmatthews/4fbcdf8e37e5d95d752b6ec9986c0fd7 to your computer and use it in GitHub Desktop.
Browser Tip Calculator
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;
};
<!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