Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
dmd-3475/tip-calculator-master/js/script.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
53 lines (46 sloc)
1.75 KB
This file contains 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
/** | |
* Get references to each of the form inputs | |
*/ | |
let total = document.querySelector('#total'); | |
let tip = document.querySelector('#tip'); | |
let people = document.querySelector('#people'); | |
/** | |
* Whenever the fields are updated, we call | |
* the updateValues function. | |
*/ | |
total.addEventListener('change', updateValues); | |
total.addEventListener('keyup', updateValues); | |
tip.addEventListener('change', updateValues); | |
tip.addEventListener('keyup', updateValues); | |
people.addEventListener('change', updateValues); | |
people.addEventListener('keyup', updateValues); | |
/** | |
* We will need references for the span tags | |
* that we will use to output our results. | |
*/ | |
let tipOutput = document.querySelector(".tipOutput"); | |
let totalOutput = document.querySelector(".totalOutput"); | |
/** | |
* This function is run every time an input is changed | |
* by a user. | |
*/ | |
function updateValues(event){ | |
// Get the latest values from our form inputs | |
let totalValue = total.value; | |
let tipValue = tip.value; | |
let peopleValue = people.value; | |
// Update the values on the screen | |
tipOutput.innerText = calculateTipPerPerson(totalValue, tipValue, peopleValue); | |
totalOutput.innerText = calculateTotalPerPerson(totalValue, tipValue, peopleValue); | |
} | |
/** | |
* Your code goes down here ... | |
* @todo Write functions for calculateTipPerPerson() and calculateTotalPerPerson() | |
*/ | |
function calculateTipPerPerson(billTotal, tipPercent, numPeople) { | |
return (Math.ceil((billTotal * tipPercent) / numPeople) /100).toFixed(2); | |
} | |
function calculateTotalPerPerson(billTotal, tipPercent, numPeople) { | |
console.log(calculateTipPerPerson(billTotal, tipPercent, numPeople)); | |
return (Math.ceil((+calculateTipPerPerson(billTotal, tipPercent, numPeople) + (billTotal / numPeople)) * 100) / 100).toFixed(2); | |
} |