JavaScript 2022

Term 3 Wednesday

May - July 2022

This site will be updated live during the sessions

Week 8 13 Apply 3 Drinks

		
001window.onload = ()=>{
		
002
		
003	// input parameters match array elements APPLYed
		
004	// price from object
		
005	// Boolean from object if false set sugar tax to 0
		
006	// price from object used with vat % from array 
		
007	const getFullPrice = function (sugarTax, vat) {		
		
008		return this.price + 
		
009		(this.sugar?sugarTax:0) + 
		
010		(this.price / 100 * vat);
		
011	}
		
012	
		
013	//object 1
		
014	const cokeOriginal = {
		
015		price: .60,
		
016		sugar: true
		
017	};
		
018	
		
019	//object 2
		
020	const cokeZero = {
		
021		price: .60,
		
022		sugar: false
		
023	};
		
024	
		
025	// array with sugar tax and vat%
		
026	const taxes = [.08, 20];
		
027	
		
028	//apply function to objects and assign to consts
		
029	const cokeOTotal = getFullPrice.apply(cokeOriginal, taxes);
		
030	const cokeZTotal = getFullPrice.apply(cokeZero, taxes);
		
031	
		
032	//log results
		
033	console.log('Coke Original £'+ cokeOTotal.toFixed(2));
		
034	console.log('Coke Zero £'+ cokeZTotal.toFixed(2));
		
035
		
036}