// ch29 Loops and Iteration 

for(var i =0; i<10; i++){
	console.log(i)
}
// -> while i is 0(i=0), and less then 10(i<10), add 1 to i(i++) until it reach 10 

for(var i=1; i<=20; i+=2){
	console.log(i);
}

var john = ["John", "Smith", 1990, "teacher", false, "blue"];
for(var i=0; i<=john.length; i++){
	console.log(john[i]);
}
//=== while loop
var i = 0;
while( i < john.length ){
	console.log(john[i]);
	i++;
}


- continue and break statments

var john = ["John", "Smith", 1990, "teacher", false, "blue"];
for(var i=0; i<=john.length; i++){
	if (typeof john[i] !== "string") continue; 
	// if values are not string, keep continue this if line again. dont' go the print line down there/ 
	console.log(john[i]);
}
// printed
// John
// Smith
// teacher
// blue
var john = ["John", "Smith", 1990, "teacher", false, "blue"];
for(var i=0; i<=john.length; i++){
	if (typeof john[i] !== "string") break; 
	// if values are not string, STOP at this line. 
	console.log(john[i]);
}
// printed
// John
// Smith


- looping backwards

for (var i = john.length-1; i >= 0; i--){ 
console.log(john[i]); 
} 

 

 



// ch30 coding challenge

/*
Remember the tip calculator challenge? Let's create a more advanced version using everything we learned!

This time, John and his family went to 5 different restaurants. 
The bills were $124, $48, $268, $180 and $42.
John likes to tip 20% of the bill when the bill is less than $50, 
15% when the bill is between $50 and $200, 
and 10% if the bill is more than $200.

Implement a tip calculator using objects and loops:
1. Create an object with an array for the bill values
2. Add a method to calculate the tip
3. This method should include a loop to iterate over
all the paid bills and do the tip calculations
4. As an output, create 1) a new array containing all tips, and 
2) an array containing final paid amounts (bill + tip). 
HINT: Start with two empty arrays [] as properties and then fill them up in the loop.


EXTRA AFTER FINISHING: Mark's family also went on a holiday, going to 4 different restaurants. 
The bills were $77, $375, $110, and $45.
Mark likes to tip 20% of the bill when the bill is less than $100, 
10% when the bill is between $100 and $300, 
and 25% if the bill is more than $300 (different than John).

5. Implement the same functionality as before, this time using Mark's tipping rules
6. Create a function (not a method) to calculate the average of a given array of tips. 
HINT: Loop over the array, and in each iteration store the current sum in a variable (starting from 0). 
After you have the sum of the array, 
divide it by the number of elements in it (that's how you calculate the average)
7. Calculate the average tip for each family
8. Log to the console which family paid the highest tips on average


*/


var john={
	fullName:"John Smith",	
	bills:[124,48,268,180,42],
	
	calcTips: function(){
		this.tips=[];
		this.total=[];

		for(var i=0; i<this.bills.length; i++){
			var percentage;
			var bill= this.bills[i];

			if(bill < 50){
				percentage=.2;
			} else if (bill >= 50 && bill <200) {
				percentage=.15;
			} else {
				percentage=.1;
			}
			
			this.tips[i]= bill*percentage;
			this.total[i] = bill+ bill*percentage;

			}
		}
	}


var mark ={
	fullName:"Mark Miller",	
	bills:[77,475,110,45],
	calcTips: function(){
		this.tips=[];
		this.total=[];

		for(var i=0; i<this.bills.length; i++){
			var percentage;
			var bill= this.bills[i];

			if(bill < 100){
				percentage=.2;
			} else if (bill >= 100 && bill <300) {
				percentage=.1;
			} else {
				percentage=.25;
			}
			
			this.tips[i]= bill*percentage;
			this.total[i] = bill+ bill*percentage;

			}
		}
	}

function calcAvg(tips){
	var sum=0;
	for(var i=0; i< tips.length; i++){
		sum=sum+tips[i];
	}
	return sum / tips.length;
}



john.calcTips();
mark.calcTips();

john.avg= calcAvg(john.tips);
mark.avg= calcAvg(mark.tips);
console.log(john,mark);



if(john.avg> mark.avg){
	console.log(john.fullName+" pays higher tips, with an average of $"+john.avg);
}else if (john.avg<mark.avg){
	console.log(mark.fullName+" pays higher tips, with an average of $"+mark.avg);
}

'javascript' 카테고리의 다른 글

JavaScript/ Scoping/ 변수영역  (0) 2020.02.24
JavaScript/ Hoisting, Function and Variable  (0) 2020.02.14
JavaScript/ Objects and Methods  (0) 2020.02.13
JavaScript/ Object and Properties  (0) 2020.02.13
JavaScript/ Array Basic  (0) 2020.02.13