// 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.")
// }