diff --git a/week-05/tip-calculator-master/js/script.js b/week-05/tip-calculator-master/js/script.js index 636f3ac..960b1f4 100644 --- a/week-05/tip-calculator-master/js/script.js +++ b/week-05/tip-calculator-master/js/script.js @@ -44,38 +44,30 @@ function updateValues(event){ * @todo Write functions for calculateTipPerPerson() and calculateTotalPerPerson() */ -function calculateTipPerPerson(totalBill, tipPercentage, numberOfPeople) { - // Calculate total tip - const totalTip = totalBill * (tipPercentage / 100); - - // Calculate tip per person - const tipPerPerson = totalTip / numberOfPeople; - - // Round to dollars and cents - const roundedTipPerPerson = Math.round(tipPerPerson * 100) / 100; - - return roundedTipPerPerson; -} - -// Function to calculate total per person -function calculateTotalPerPerson(totalBill, tipPercentage, numberOfPeople) { - // Calculate total tip - const totalTip = totalBill * (tipPercentage / 100); - - // Calculate total amount including tip - const totalAmount = totalBill + totalTip; - - // Calculate total per person - const totalPerPerson = totalAmount / numberOfPeople; - - // Round to dollars and cents - const roundedTotalPerPerson = Math.round(totalPerPerson * 100) / 100; - - return roundedTotalPerPerson; -} - -// Example usage within the updateValues() function: -function updateValues(totalBill, tipPercentage, numberOfPeople) { - const tipPerPerson = calculateTipPerPerson(totalBill, tipPercentage, numberOfPeople); - const totalPerPerson = calculateTotalPerPerson(totalBill, tipPercentage, numberOfPeople); +function calculateTipPerPerson(totalValue, tipValue, peopleValue) { + const tipAmount = total * (tipValue / 100); + const tipPerPerson = tipAmount / peopleValue; + return Math.round(tipPerPerson * 100) / 100; // round to 2 decimal places + } + + function calculateTotalPerPerson(totalValue, tipValue, peopleValue) { + const tipAmount = totalValue * (tipValue / 100); + const totalPerPerson = (totalValue + tipAmount) / peopleValue; + return Math.round(totalPerPerson * 100) / 100; / + } + + // example usage: + function updateValues() { + const totalValue = parseFloat(document.getElementById('totalValue').value); + const tipValue = parseFloat(document.getElementById('tipValue').value); + const peopleValue = parseInt(document.getElementById('peopleValue').value); + + const tipPerPerson = calculateTipPerPerson(totalValue, tipValue, peopleValue); + const totalPerPerson = calculateTotalPerPerson(totalValue, tipValue, peopleValue); + + document.getElementById('tip-per-person').textContent = `$${tipPerPerson.toFixed(2)}`; + document.getElementById('total-per-person').textContent = `$${totalPerPerson.toFixed(2)}`; + } + +