Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
week5
  • Loading branch information
kee21001 committed Mar 17, 2024
1 parent 8d550e3 commit e6ec72d
Showing 1 changed file with 26 additions and 34 deletions.
60 changes: 26 additions & 34 deletions week-05/tip-calculator-master/js/script.js
Expand Up @@ -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)}`;
}



0 comments on commit e6ec72d

Please sign in to comment.