// 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

// ch20 Function 

function calcilateAge(birthYear){// you can also pass several var here
	return 2018-birthYear;
}

var ageJohn=calcilateAge(1990);// you just CALL function
var ageMike=calcilateAge(1948);
var ageJane=calcilateAge(1989);
console.log(ageJohn, ageMike, ageJane);// console printed // 28


function yearsUntilRetirement(year, firstName){
	var age = calcilateAge(year);
	var retire = 65 - age;
	if(retire>0){
	console.log(firstName+" retires in "+ retire+" years." );
}else{
	console.log(firstName+ " is already retired.");
}
}

yearsUntilRetirement(1990, "John"); // console printed // John retires in 37 years.
yearsUntilRetirement(1948, "Mike"); //Mike is already retired.
yearsUntilRetirement(1989, "Jane");





// ch21 Function Statements and Expressions

- function declairation
ex) function whatDoYouDo(job,firstName){}


- function expression
ex)

2+3, typeof...
-> they bring out immediate results.

var whatDoYouDo = function (job, firstName){
	switch (job){
		case "teacher":
			return firstName +" teaches kids how to code.";
		case "driver":
			return firstName +" drives a taxi.";
		case "designer":
			return firstName +" designs amazing clothes.";
		default:
			return firstName+" does nothing."
	}
}

console.log(whatDoYouDo("teacher", "John"));
console.log(whatDoYouDo("designer", "Jane"));
console.log(whatDoYouDo("none", "Jakob"));


- function statements: they don't bring out the result immediately.

'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/ Array Basic  (0) 2020.02.13
JavaScript/ Basic  (0) 2020.02.13

// Variables and data types

var firstName = 'John';
console.log(firstName);

var lastName = 'Smith';
var age = 28;

var fullAge = true;
console.log(fullAge);

var job;
console.log(job);

job = 'Teacher';
console.log(job);

// Variable naming rules
var _3years = 3;
var johnMark = 'John and MArk';
var if = 23;

 

 

 

// Variable mutation and type coercion

var firstName = 'John';
var age = 28;

// Type coercion
console.log(firstName + ' ' + age);

var job, isMarried;
job = 'teacher';
isMarried = false;

console.log(firstName + ' is a ' + age + ' year old ' + job + '. Is he married? ' + isMarried);

// Variable mutation
age = 'twenty eight';
job = 'driver';

alert(firstName + ' is a ' + age + ' year old ' + job + '. Is he married? ' + isMarried);

var lastName = prompt('What is his last Name?');
console.log(firstName + ' ' + lastName);

 

 

 

// Basic operators

var year, yearJohn, yearMark;
now = 2018;
ageJohn = 28;
ageMark = 33;

// Math operators
yearJohn = now - ageJohn;
yeahMark = now - ageMark;

console.log(yearJohn);

console.log(now + 2);
console.log(now * 2);
console.log(now / 10);


// Logical operators
var johnOlder = ageJohn < ageMark;
console.log(johnOlder);


// typeof operator
console.log(typeof johnOlder);
console.log(typeof ageJohn);
console.log(typeof 'Mark is older tha John');
var x;
console.log(typeof x);

 

 

 

// Operator precedence

var now = 2018;
var yearJohn = 1989;
var fullAge = 18;

// Multiple operators
var isFullAge = now - yearJohn >= fullAge; // true
console.log(isFullAge);

// Grouping
var ageJohn = now - yearJohn;
var ageMark = 35;
var average = (ageJohn + ageMark) / 2;
console.log(average);

// Multiple assignments
var x, y;
x = y = (3 + 5) * 4 - 6; // 8 * 4 - 6 // 32 - 6 // 26
console.log(x, y);


// More operators
x *= 2;
console.log(x);
x += 10;
console.log(x);
x--;
console.log(x);

 

 

 

// ch13 basic operators code challenge

/*
1. Store Mark's and John's mass and height in variables
2. Calculate both their BMIs
3. Create a boolean variable containing information about whether Mark has a higher BMI than John.
4. Print a string to the console containing the variable from step 3. (Something like "Is Mark's BMI higher than John's? true"). 

*/

var massJohn=70; 
var massMark=95; 
var heightJohn=1.8; 
var heightMark=1.75; 

var BMIJohn=massJohn/(heightJohn*heightJohn); 
var BMIMark=massMark/(heightMark*heightMark); 

var isHigher=BMIMark>BMIJohn; 
console.log("Is Mark's BMI higher than John's? "+isHigher) 





// ch14 if else statements 

var firstName="John";
var civilStatus="Single";

if(civilStatus==="married"){
	console.log(firstName + " is married.");
}else{
	console.log(firstName+" is Single.");
}


var isMarried=true;
if(isMarried){
	console.log(firstName + " is married.");
}else{
	console.log(firstName+" is Single.");
}

//////////////////////

var massJohn=70;
var massMark=95;
var heightJohn=1.8;
var heightMark=1.75;

var BMIJohn=massJohn/(heightJohn*heightJohn);
var BMIMark=massMark/(heightMark*heightMark);

if(BMIMark>BMIJohn){
	console.log("Mark's BMI is higher than John's.")
}else{
	console.log("John's BMI is higher than Mark's.")
} 





// ch15  Boolean Logic

var firstName="John";
var age=20;

if(age<13){
	console.log(firstName+" is a boy.")
}else if(age>=13 && 20>age){
	console.log(firstName+" is a teenager.")
}else if(age>=20 && 30> age){
	console.log(firstName+" is a young man.")
}else{
	console.log(firstName+" is a man.")
}





// ch16 the ternary operator and switch statements 

var firstName="John";
var age=22;

//ternary operator
age>=18? console.log(firstName+ " drinks beer."):console.log(firstName+" drinks juice."); 

var drink =age>=18? "beer":"juice"; 
console.log(drink);//beer

//switch statement
var job="teacher";
switch(job){
	case "teacher":
	case "instructor"
		console.log(firstName +" theaches kids how to code.");
		break;
	case "driver":
		console.log(firstName+" drives an uber in Lisbon.");
		break;
	case "designer":
		console.log(firstName+" designs beautiful websites.");
		break;
	default:
		console.log(firstName+ "does nothing.");
}

switch(true){
	case age<13:
		console.log(firstName+" is a boy.");
		break;
	case age>=13 && 20>age:
		console.log(firstName+" is a teenager.");
		break;
	case age>=20 && 30> age:
		console.log(firstName+" is a young man.");
		break;
	default:
		console.log(firstName+" is a man.");
}





// ch17 Truthy and Falsy values and equality operators 

- falsy values: undefined, null, 0, " '', NaN
- truthy values: all the values that NOT falsy

var height;

if(height){
	console.log("variable is defined.");
}else{
	console.log("variable has not defined");
}

// equality operators(===는 변수타입까지 비교)
height=23;
if(height=="23"){
	console.log("the == operator does type coercion!")
}
//23=="23"
//true
//23==="23"
//false





// ch18 coding challenge

/*
John and Mike both play basketball in different teams. In the latest 3 games, 
John's team scored 89, 120 and 103 points, while Mike's team scored 116, 94 and 123 points.

1. Calculate the average score for each team
2. Decide which teams wins in average (highest average score), 
   and print the winner to the console. Also include the average score in the output.
3. Then change the scores to show different winners. 
   Don't forget to take into account there might be a draw (the same average score)

4. EXTRA: Mary also plays basketball, and her team scored 97, 134 and 105 points. 
   Like before, log the average winner to the console. 
   HINT: you will need the && operator to take the decision. 
   If you can't solve this one, just watch the solution, it's no problem :)
5. Like before, change the scores to generate different winners, 
   keeping in mind there might be draws.

*/

var teamJohnAvg= (89+120+103)/3;
var teamMikeAvg= (116+94+124)/3;
var teamMaryAvg=(97+134+105)/3;

if(teamJohnAvg>teamMikeAvg && teamJohnAvg>teamMaryAvg){
	console.log("John's team wins with "+teamJohnAvg+" scores.")
}else if(teamMikeAvg>teamJohnAvg && teamMikeAvg>teamMaryAvg){
	console.log("Mike's team wins with "+teamMikeAvg+" scores.");
}else if (teamMaryAvg>teamJohnAvg&&teamMaryAvg>teamMikeAvg) {
	console.log("Mary's team wins with "+teamMaryAvg+" scores.");
}else{
	console.log("they are draw.")
}

// if(teamMikeAvg>teamJohnAvg){
// 	console.log("Mike's team wins with "+teamMikeAvg+" scores.");
// }else if(teamMikeAvg<teamJohnAvg){
// 	console.log("John's team wins with "+teamJohnAvg+" scores.")
// }else{
// 	console.log("they are draw.")
// }



'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/ Array Basic  (0) 2020.02.13
JavaScript/ Function Basic  (0) 2020.02.13