2024-12-18 19:45:34 +00:00

386 lines
7.5 KiB
JavaScript

//Javascript Tutorial
// Means Comment
/* hehhe
wow this is fun
i am dumb */
// Hello I
// can
// do this now
// variable (var): want to make a empty box called dog, but expecting sth in there later
// Can later assign Lucy to this box. variable originally empty box name is dog, but then the variable value is Lucy
var dog;
dog = "Buddy";
console.log(dog);
// var is the old name, it is a bad practice
//let: will change, e.g. box called cat but give it a different cat (value) every single time
let cat;
cat = "Tom";
console.log(cat);
cat = "John";
console.log(cat);
//const: constant, thing in box will never change
const bird = "sparrow";
console.log(bird);
//Data types: type of things u put in variable, e.g. tell box to put in specific type of value
//Primitives Data Types
//String, Number, Boolean, Undefined, Null
//String: anything in single or double quotation, can type anything, show direct thing in quotation
const funnystring = "John Doe";
console.log(funnystring);
//String concatenation
const name1 = "John";
const name2 = "Doe";
const fullName = `The full name is ${name1} ${name2}`;
console.log (fullName);
console.log ("The full name is" + name1 + " " + name2);
//String Escape Character, \ escapes the quotation
console.log ("Ronald said \"It'\s raining outside\"");
//String indexing: the computer recognizes it as each number assigned: every single letter on the word will have a index
const string = "javascript bob";
console.log(string[0]); //j
console.log(string[5]);
console.log(string[11]);
// Number: just a number (both one with decimal or one without)
let age = 20;
console.log (age);
age = age + 15;
console.log (age)
//NaN: not a number
let result = "Hello"/2;
console.log(result);
//infinity
let infinity = 1/0
console.log(infinity)
//Boolean: true or false statements, all comparisons, !==" "
console.log (1>2);
console.log (5<7);
console.log ("tiffany" !== "Tiffany")
let tiffany = "tiffany"
let haha = "Tiffany"
tiffany = haha
console.log (tiffany == haha)
//Undefined: did not give x anything
let x;
console.log(x);
//Null: telling them theres nothing in the code
let y = null;
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";
const chalk = require('chalk')
console.log(chalk.red('Hello World!'))
//Extra exercise
//printPyramid(5)
// Expected output
// 1
// 1 2
// 1 2 3
// 1 2 3 4
// 1 2 3 4 5
const printPyramid = (n) => {
for (let i = 1; i <= n; i++) {
let row = '';
for (let j = 1; j <= i; j++) {
row += j + ' ';
}
console.log(row);
}
}
printPyramid(10)
// fibonacci(8) // Expected output: 0 1 1 2 3 5 8 13
const fibonacci = (n) => {
let i = 0;
let j = 1;
console.log(i);
console.log(j);
for (let count = 2; count < n; count++) {
let next = i + j;
console.log(next);
i = j;
j = next;
}
};
fibonacci(8)