lesson 2
This commit is contained in:
parent
0574d9bc7a
commit
3b2c082808
@ -85,4 +85,255 @@ console.log(x);
|
||||
|
||||
//Null: telling them theres nothing in the code
|
||||
let y = null;
|
||||
console.log(y);
|
||||
console.log(y);
|
||||
|
||||
//Non-primitive data type: anything thats not within the box//
|
||||
//Object, Array, Function//
|
||||
|
||||
//Object: sth to define a list of things, e.g. list of primitive data types//
|
||||
// const or let -> {curly}, can contain diff. data types
|
||||
|
||||
const user = {
|
||||
name1: "John",
|
||||
age: 5,
|
||||
verified: true,
|
||||
|
||||
//Nested object (within the object)
|
||||
address: {
|
||||
city: "New York",
|
||||
state: "NY",
|
||||
}
|
||||
}
|
||||
|
||||
console.log(user)
|
||||
console.log(user.address.state)
|
||||
|
||||
//Array: a list of primitive data types within one category, only one type of data
|
||||
const fruits = ["Apple", "Banana", "Cherry", "Dates"];
|
||||
const numbers = [1, 2, 3, 4, 5];
|
||||
|
||||
//Array, has string indexing stuff
|
||||
console.log(fruits[2]) // Cherry
|
||||
|
||||
//Function
|
||||
// y = f(x) = 2x + 1
|
||||
// y -output, x-input
|
||||
|
||||
function add(x) {
|
||||
return 2*x + 1;
|
||||
}
|
||||
|
||||
add (5);
|
||||
console.log(add(5)); //11
|
||||
|
||||
//greet is the function name
|
||||
// ${}: must use `(next to 1)
|
||||
function greet(name) {
|
||||
return `Hello, ${name}`;
|
||||
}
|
||||
console.log(greet('Tiffany')); // Hello Tiffany
|
||||
|
||||
let functionName = function(x) {
|
||||
return 2*x + 1;
|
||||
}
|
||||
|
||||
//best practice to declare a function
|
||||
const arrowFunction = (x) => {
|
||||
return 2*x + 1;
|
||||
}
|
||||
|
||||
//Operators, things we can do with the data types
|
||||
|
||||
//Arithmetic operators
|
||||
// e.g. + - * / ++ --
|
||||
|
||||
let a = 10;
|
||||
let b = 5;
|
||||
|
||||
console.log(a + b); //15
|
||||
console.log(a - b); //5
|
||||
console.log(a * b); //50
|
||||
console.log(a/b); //2
|
||||
console.log(a % b) //0 (remainder of division)
|
||||
console.log(a++); //11 (a = a + 1)
|
||||
console.log(a--); //9 (a = a-1)
|
||||
|
||||
//Assignment operators
|
||||
// =, +=, -=, *=, /=, %=
|
||||
|
||||
let c = 10
|
||||
let d = 5
|
||||
|
||||
c += d; //c = c + d
|
||||
|
||||
//Comparison operators, comparing stuffs
|
||||
// ==, !=, >, <, >=, <=, ===, !==
|
||||
|
||||
let e = "10"
|
||||
let f = 10
|
||||
|
||||
console.log(e == f); // false (is e equal to f)
|
||||
|
||||
console.log(e === f) //false, absolutely equal to, which it is not, but if ==
|
||||
|
||||
//Logical Operators
|
||||
//&&(AND), ||(OR), ! (NOT)
|
||||
|
||||
let g = 10;
|
||||
let h = 5;
|
||||
|
||||
console.log(g > 5 && h > 10); //false
|
||||
console.log(g > 5 || h > 10); //true
|
||||
console.log(!(g > 5)); //false, just reverse whats inside
|
||||
|
||||
//Control structures
|
||||
//If statements
|
||||
|
||||
// if (condition) {
|
||||
// //result
|
||||
// }
|
||||
|
||||
let i = 2;
|
||||
|
||||
if (i > 5) {
|
||||
console.log("I am smart")
|
||||
}
|
||||
|
||||
//If-Else statement
|
||||
let j = 10;
|
||||
|
||||
if (j > 5) {
|
||||
console.log("Ronald believes in Tiffany's religion")
|
||||
} else {
|
||||
console.log("Ronald does not believe in Tiffany's religion")
|
||||
}
|
||||
|
||||
// If Else If statement
|
||||
let k = 10
|
||||
|
||||
if (k > 5) {
|
||||
console.log("k is larger than 5");
|
||||
} else if (k < 5) {
|
||||
console.log("k is less than 5");
|
||||
} else if (k === 5) {
|
||||
console.log("k is equal to 5");
|
||||
} else {
|
||||
console.log("k is not a number");
|
||||
}
|
||||
|
||||
//Different way to announce if statement, ternary operator
|
||||
|
||||
let n = 10;
|
||||
//condition ?(if) expression1(true) : expression 2(false)
|
||||
n > 5 ? console.log("n is greater than 5") : console.log("n is smaller than 5");
|
||||
|
||||
// Switch statement
|
||||
let day = "Monday";
|
||||
|
||||
switch(day) {
|
||||
case "Monday":
|
||||
console.log("Today is Monday \:\(")
|
||||
break;
|
||||
case "Tuesday":
|
||||
console.log("Today is Tuesday")
|
||||
break;
|
||||
default:
|
||||
console.log("No day")
|
||||
break;
|
||||
}
|
||||
|
||||
// switch(condition) {
|
||||
// case value1:
|
||||
// //code block
|
||||
// break; (end of this case)
|
||||
// case value2:
|
||||
// //code block
|
||||
// break;
|
||||
// default:
|
||||
// //code block
|
||||
// break;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
//Loops, usually a number to loop
|
||||
|
||||
// For Loop
|
||||
// for (initialization; condition; increment/decrement) {
|
||||
// //code block
|
||||
// }
|
||||
|
||||
for (let i = 0; i < 5; i++) {
|
||||
console.log("Tiffany is smart");
|
||||
}
|
||||
|
||||
// While Loop, anything within the condition, if it's still true, will keep running
|
||||
// while(condition) {
|
||||
// // code block
|
||||
// }
|
||||
|
||||
let m = 0;
|
||||
|
||||
while (m<5) {
|
||||
console.log("Tiffany is smart");
|
||||
m++;
|
||||
}
|
||||
|
||||
// For In Loop, comma within object (for objects)
|
||||
let person2 = {
|
||||
name: "Tiffany",
|
||||
age: 24,
|
||||
isSmart: true,
|
||||
}
|
||||
|
||||
//key can be any name, eg color, looping the list
|
||||
for (let key in person2) {
|
||||
console.log(key, person2[key]);
|
||||
}
|
||||
|
||||
// For of Loop (for arrays)
|
||||
let numbers1 = [1, 2, 3, 4, 5]
|
||||
|
||||
for (let number of numbers1) {
|
||||
number += 1; // number= number + 1
|
||||
console.log(number);
|
||||
}
|
||||
|
||||
//Do While Loop
|
||||
|
||||
//do {
|
||||
// // code block
|
||||
// } while (condition);
|
||||
|
||||
let happy = 0
|
||||
|
||||
do {
|
||||
console.log("Tiffany is the savior");
|
||||
happy++;
|
||||
} while (happy < 5)
|
||||
|
||||
//looping "tiffany is the savior" using happy (plus 1 for each round)
|
||||
// until happy reaches a number larger than 5
|
||||
|
||||
// Classes: recipe book or instruction
|
||||
//constructor, like ingredients
|
||||
|
||||
class bread {
|
||||
constructor(flour, water, salt) {
|
||||
this.flour = flour;
|
||||
this.water = water;
|
||||
this.salt = salt;
|
||||
}
|
||||
|
||||
bake() {
|
||||
console.log(`Baking Bread with ${this.flour}, using ${this.water} cups of water and ${this.salt} tsp of salt.`);
|
||||
}
|
||||
}
|
||||
const bread1 = new bread("wheat", 2, 3)
|
||||
|
||||
bread1.bake()
|
||||
|
||||
// Modules: it only happens in a modules
|
||||
|
||||
//Import
|
||||
//import {greet} from "./greet.js";
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user