// ch22 Arrays 

//Initialize new array 
var names = ["John", "Mark", "Jake"]; 
var years = new Array(1990, 1980, 1956); 

console.log(names); // console printed // ["John", "Mark", "Jake"] 
console.log(names[0]); // console printed // John 
console.log(names.length); // console printed // 3 

//Mutate array data 
names[1] = "Ben"; 
names[5] = "Mary"; 
console.log(names); //printed // (6)["John", "Ben", "Jake", empty x2, "Mary"] 
//if you want put mary in last order 
names[names.length] ="Mary";//printed // (4)["John", "Ben", "Jake", "Mary"] 

//Array contains different data types 
var john = ["John", "Smith", 1990, "teacher", false]; 
john.push("blue"); //added in the ending 
john.unshift("Mr."); //added in the first 
console.log(john); //printed// ["Mr.", "John", "Smith", 1990, "teacher", false, "blue"] 

john.pop(); //remove the end 
console.log(john); //printed//["Mr.", "John", "Smith", 1990, "teacher", false] 
john.shift(); // remove the first 
console.log(john); //printed//["John", "Smith", 1990, "teacher", false] 

john.indexOf(1990); //find index number of data, if data is not in the array ift return -1. 
john.indexOf("designer") === -1 ? "John is not a designer." : "John is a designer."; 






// ch23 Challenge CalcTips

/*
John and his family went on a holiday and went to 3 different restaurants. 
The bills were $124, $48 and $268.

To tip the waiter a fair amount, John created a simple tip calculator (as a function). 
He 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.

In the end, John would like to have 2 arrays:
1) Containing all three tips (one for each bill)
2) Containing all three final paid amounts (bill + tip).

(NOTE: To calculate 20% of a value, simply multiply it with 20/100 = 0.2)

*/

function calcTips (bill){
	var percentage;

	if(bill < 50){
		percentage=.2;
	} else if (bill >= 50 && bill <200) {
		percentage=.15;
	} else {
		percentage=.1;
	}
	return percentage * bill;
}

calcTips(100); //print 15

var bills = [124, 48, 268]
var tips = [calcTips(bills[0]),
			calcTips(bills[1]),
			calcTips(bills[2])];
var total = [bills[0]+tips[0],
			 bills[1]+tips[1],
			 bills[2]+tips[2]];

console.log(tips, total);

'javascript' 카테고리의 다른 글

JavaScript/ Loops and Iteration  (0) 2020.02.13
JavaScript/ Objects and Methods  (0) 2020.02.13
JavaScript/ Object and Properties  (0) 2020.02.13
JavaScript/ Function Basic  (0) 2020.02.13
JavaScript/ Basic  (0) 2020.02.13