diff --git a/Nguyen-Kevin-JS-Leet-Practice/longestSubstring.js b/Nguyen-Kevin-JS-Leet-Practice/longestSubstring.js new file mode 100644 index 0000000..25e1840 --- /dev/null +++ b/Nguyen-Kevin-JS-Leet-Practice/longestSubstring.js @@ -0,0 +1,17 @@ +function lengthOfLongestSubstring(s) { + let windowCharsMap = {}; + let windowStart = 0; + let maxLength = 0; + + for (let i =0; i < s.length; i++) { + const endChar = s[i]; + + if(windowCharsMap[endChar] >= windowStart) { + windowStart = windowCharsMap[endChar] + 1; + } + + windowCharsMap[endChar] = i; + maxLength = Math.max(maxLength, i - windowStart + 1); + } + return maxLength; +} \ No newline at end of file diff --git a/Nguyen-Kevin-JS-Leet-Practice/palindrome.js b/Nguyen-Kevin-JS-Leet-Practice/palindrome.js new file mode 100644 index 0000000..a0f1fb3 --- /dev/null +++ b/Nguyen-Kevin-JS-Leet-Practice/palindrome.js @@ -0,0 +1,17 @@ + +function isParlindrome(s){ + s =s.toLowerCase().replace(/[^\w]/g, ""); + + let left = 0; + let right = s.length -1; + + while(left < right) { + if (s[left] !== s[right]){ + return false; + } + left++; + right--; + } + return true; +} + diff --git a/Traub-Eric-JS-Practice/10_use_strict.js b/Traub-Eric-JS-Practice/10_use_strict.js new file mode 100644 index 0000000..d5024d5 --- /dev/null +++ b/Traub-Eric-JS-Practice/10_use_strict.js @@ -0,0 +1,44 @@ +// example 1 + +/* require let, var, const + for variable declaration +*/ + +'use strict'; + +city = 'London'; +console.log(city); + + + + +// example 2 + +/* +parameters must be unique + +*/ + +'use strict'; + +function myFunc(a, a, b) { + console.log(a, a, b); +} +myFunc(1, 2, 3); + + + + +// example 3 + +/* +errors thrown for attempts to delete +native definitions +*/ + +'use strict'; + +delete Object.prototype; + + +// use strict: fail FAST & fail LOUDLY \ No newline at end of file diff --git a/Traub-Eric-JS-Practice/11_curry_function.js b/Traub-Eric-JS-Practice/11_curry_function.js new file mode 100644 index 0000000..4f2a950 --- /dev/null +++ b/Traub-Eric-JS-Practice/11_curry_function.js @@ -0,0 +1,32 @@ +// tab 1 + +// function getProduct(num1, num2) { +// return num1 * num2; +// } + +function getProduct(num1) { + return function(num2) { + return num1 * num2; + }; +} + +getProduct(10)(20); + + + + +// tab 2 + +// function getTravelTime(distance, speed) { +// return distance / speed; +// } + +function getTravelTime(distance) { + return function(speed) { + return distance / speed; + }; +} + +const travelTimeBosNyc = getTravelTime(400); +const travelTimeMiamiAtlanta = getTravelTime(600); +console.log(travelTimeBosNyc(100)); \ No newline at end of file diff --git a/Traub-Eric-JS-Practice/12_counter_function.js b/Traub-Eric-JS-Practice/12_counter_function.js new file mode 100644 index 0000000..c672cd3 --- /dev/null +++ b/Traub-Eric-JS-Practice/12_counter_function.js @@ -0,0 +1,20 @@ +function myFunc() { + let count = 0; + + return function() { + count++; + return count; + }; + } + + console.log(myFunc()); + + const instanceOne = myFunc(); + const instanceTwo = myFunc(); + + console.log('instanceOne: ', instanceOne()); + console.log('instanceOne: ', instanceOne()); + console.log('instanceOne: ', instanceOne()); + console.log('instanceTwo: ', instanceTwo()); + console.log('instanceTwo: ', instanceTwo()); + console.log('instanceOne: ', instanceOne()); \ No newline at end of file diff --git a/Traub-Eric-JS-Practice/13_logging_x_and_y.js b/Traub-Eric-JS-Practice/13_logging_x_and_y.js new file mode 100644 index 0000000..fa0764b --- /dev/null +++ b/Traub-Eric-JS-Practice/13_logging_x_and_y.js @@ -0,0 +1,8 @@ +"use strict"; + +(function() { + var x = y = 200; +})(); + +console.log('y: ', y); +console.log('x: ', x); \ No newline at end of file diff --git a/Traub-Eric-JS-Practice/14_call_and_apply.js b/Traub-Eric-JS-Practice/14_call_and_apply.js new file mode 100644 index 0000000..2db210d --- /dev/null +++ b/Traub-Eric-JS-Practice/14_call_and_apply.js @@ -0,0 +1,18 @@ +const car1 = { + brand: 'Porsche', + getCarDescription: function(cost, year, color) { + console.log(`This car is a ${this.brand}. The price is $${cost}. The year is ${year}. The color is ${color}.\n`); + } +}; + +const car2 = { + brand: 'Lamborghini' +}; + +const car3 = { + brand: 'Ford' +}; + +car1.getCarDescription(80000, 2010, 'blue'); +car1.getCarDescription.call(car2, 200000, 2013, 'yellow'); +car1.getCarDescription.apply(car3, [35000, 2012, 'black']); \ No newline at end of file diff --git a/Traub-Eric-JS-Practice/15_determine_list2.js b/Traub-Eric-JS-Practice/15_determine_list2.js new file mode 100644 index 0000000..dbe3a31 --- /dev/null +++ b/Traub-Eric-JS-Practice/15_determine_list2.js @@ -0,0 +1,8 @@ +const list1 = [1, 2, 3, 4, 5]; +const list2 = list1.slice(); +// const list2 = list1.concat([]); + +list1.push(6, 7, 8); + +console.log('List 1: ', list1); +console.log('List 2: ', list2); \ No newline at end of file diff --git a/Traub-Eric-JS-Practice/16_singly_doubly_invoked_functions.js b/Traub-Eric-JS-Practice/16_singly_doubly_invoked_functions.js new file mode 100644 index 0000000..901eeaa --- /dev/null +++ b/Traub-Eric-JS-Practice/16_singly_doubly_invoked_functions.js @@ -0,0 +1,17 @@ +function getTotal() { + var args = Array.prototype.slice.call(arguments); + + if (args.length === 2) { + return args[0] + args[1]; + } + else if (args.length === 1) { + return function(num2) { + return args[0] + num2; + }; + } + } + + console.log(getTotal(10, 20)); + console.log(getTotal(5, 40)); + console.log(getTotal(3)(30)); + console.log(getTotal(8)(12)); \ No newline at end of file diff --git a/Traub-Eric-JS-Practice/17_json_data.js b/Traub-Eric-JS-Practice/17_json_data.js new file mode 100644 index 0000000..722ed53 --- /dev/null +++ b/Traub-Eric-JS-Practice/17_json_data.js @@ -0,0 +1,17 @@ +// TASK: +// 1. Describe what JSON format is. +// 2. Delete the data types not permitted in JSON. +// 3. Replace placeholder-text with the corresponding data type, +// properly formatted as JSON. + +const myJsonObj = { + "myString": "hello world", + "myNumber": 12345.6789, + "myNull": null, + "myBoolean": true, + "myArray": [20, 30, "orange"], + "myObject": { + "name": "Sam", + "age": 30 + } + }; \ No newline at end of file diff --git a/Traub-Eric-JS-Practice/18_order_logged_out.js b/Traub-Eric-JS-Practice/18_order_logged_out.js new file mode 100644 index 0000000..2666ce6 --- /dev/null +++ b/Traub-Eric-JS-Practice/18_order_logged_out.js @@ -0,0 +1,8 @@ +function logNumbers() { + console.log(1); + setTimeout(function(){console.log(2)}, 1000); + setTimeout(function(){console.log(3)}, 0); + console.log(4); +} + +logNumbers(); \ No newline at end of file diff --git a/Traub-Eric-JS-Practice/19_creating_objects.js b/Traub-Eric-JS-Practice/19_creating_objects.js new file mode 100644 index 0000000..ded5ef3 --- /dev/null +++ b/Traub-Eric-JS-Practice/19_creating_objects.js @@ -0,0 +1,38 @@ +// object literal syntax +const myBoat = { + length: 24, + maxSpeed: 45, + passengers: 14, + getLength: function() { + return this.length; + } + }; + + + // new keyword & Object constructor + const student = new Object(); + + student.grade = 12; + student.gradePointAverage = 3.7; + student.classes = ["English", "Algebra", "Chemistry"]; + student.getClasses = function() { + return this.classes; + }; + + + // constructor function + function Car(color, brand, year) { + this.color = color; + this.brand = brand; + this.year = year; + } + + Car.prototype.getColor = function() { + return this.color; + }; + + const carlysCar = new Car('blue', 'ferarri', 2015); + const jimsCar = new Car('red', 'tesla', 2014); + + console.log(carlysCar.getColor()); + console.log(jimsCar.getColor()); \ No newline at end of file diff --git a/Traub-Eric-JS-Practice/1_tripleAdd.js b/Traub-Eric-JS-Practice/1_tripleAdd.js new file mode 100644 index 0000000..73920b1 --- /dev/null +++ b/Traub-Eric-JS-Practice/1_tripleAdd.js @@ -0,0 +1,9 @@ +tripleAdd(10)(20)(30); // 60 + +function tripleAdd(num1) { + return function(num2) { + return function(num3) { + return num1 + num2 + num3; + } + } +} diff --git a/Traub-Eric-JS-Practice/20_typeof_data_types.js b/Traub-Eric-JS-Practice/20_typeof_data_types.js new file mode 100644 index 0000000..8fd20c6 --- /dev/null +++ b/Traub-Eric-JS-Practice/20_typeof_data_types.js @@ -0,0 +1,6 @@ +console.log(typeof null); +console.log(typeof undefined); +console.log(typeof {}); +console.log(typeof []); +console.log(Array.isArray([])); +console.log([] instanceof Array); \ No newline at end of file diff --git a/Traub-Eric-JS-Practice/2_iife.js b/Traub-Eric-JS-Practice/2_iife.js new file mode 100644 index 0000000..356033e --- /dev/null +++ b/Traub-Eric-JS-Practice/2_iife.js @@ -0,0 +1,7 @@ +// immediately invoked function + +(function doubleNumber(num){ + return num * 2; +})(10); + +doubleNumber(5); \ No newline at end of file diff --git a/Traub-Eric-JS-Practice/3_button_5.js b/Traub-Eric-JS-Practice/3_button_5.js new file mode 100644 index 0000000..4904551 --- /dev/null +++ b/Traub-Eric-JS-Practice/3_button_5.js @@ -0,0 +1,56 @@ +// function createButtons() { +// for (var i = 1; i <= 5; i++) { +// var body = document.getElementsByTagName("BODY")[0]; +// var button = document.createElement("BUTTON"); +// button.innerHTML = 'Button ' + i; +// button.onclick = function() { +// alert('This is button ' + i); +// } +// body.appendChild(button); +// } +// } + +// createButtons(); + + // Solution 2: + +function createButtons() { + for (var i = 1; i <= 5; i++) { + var body = document.getElementsByTagName("BODY")[0]; + var button = document.createElement("BUTTON"); + button.innerHTML = 'Button ' + i; + // (function(num) { + // button.onclick = function() { + // alert('This is button ' + num); + // }; + // })(i) + addClickFunctionality(button, i); + body.appendChild(button); + } + } + + createButtons(); + + + function addClickFunctionality(button, num) { + button.onclick = function() { + alert('This is button ' + num); + } + } + + // Solution 3: + // let is block scoped instead of function scoped + +function createButtons() { + for (let i = 1; i <= 5; i++) { + var body = document.getElementsByTagName("BODY")[0]; + var button = document.createElement("BUTTON"); + button.innerHTML = 'Button ' + i; + button.onclick = function() { + alert('This is button ' + i); + } + body.appendChild(button); + } + } + + createButtons(); \ No newline at end of file diff --git a/Traub-Eric-JS-Practice/4_closure.js b/Traub-Eric-JS-Practice/4_closure.js new file mode 100644 index 0000000..e69de29 diff --git a/Traub-Eric-JS-Practice/5_this.js b/Traub-Eric-JS-Practice/5_this.js new file mode 100644 index 0000000..e69de29 diff --git a/Traub-Eric-JS-Practice/7_self.js b/Traub-Eric-JS-Practice/7_self.js new file mode 100644 index 0000000..8d4966c --- /dev/null +++ b/Traub-Eric-JS-Practice/7_self.js @@ -0,0 +1,22 @@ +var myCar = { + color: "Blue", + logColor: function() { + var self = this; + console.log("In logColor - this.color: " + this.color); + console.log("In logColor - self.color: " + self.color); + (function() { + console.log("In IIFE - this.color: " + this.color); + console.log("In IIFE - self.color: " + self.color); + })(); + } +}; + +myCar.logColor(); + + +/* +blue +blue +undefined +blue +*/ \ No newline at end of file diff --git a/Traub-Eric-JS-Practice/8_equals_vs_strict.js b/Traub-Eric-JS-Practice/8_equals_vs_strict.js new file mode 100644 index 0000000..3c1c954 --- /dev/null +++ b/Traub-Eric-JS-Practice/8_equals_vs_strict.js @@ -0,0 +1,15 @@ +console.log(7 == '7'); // true + +console.log(7 === '7'); // false + + +/* +== + +num == str // string converted to num + +boolean == non-boolean // non-boolean converted to boolean + +object == primitive // object converted to primitive + +*/ \ No newline at end of file diff --git a/Traub-Eric-JS-Practice/9_log_numer.js b/Traub-Eric-JS-Practice/9_log_numer.js new file mode 100644 index 0000000..016e719 --- /dev/null +++ b/Traub-Eric-JS-Practice/9_log_numer.js @@ -0,0 +1,8 @@ +var num = 50; + +function logNumber() { + console.log(num); + var num = 100; +} + +logNumber(); \ No newline at end of file diff --git a/Traversy_Brad_Modern_JS/index.html b/Traversy_Brad_Modern_JS/index.html new file mode 100644 index 0000000..f15b337 --- /dev/null +++ b/Traversy_Brad_Modern_JS/index.html @@ -0,0 +1,13 @@ + + + + + + Document + + +

Objects

+ + + + \ No newline at end of file diff --git a/Traversy_Brad_Modern_JS/script.js b/Traversy_Brad_Modern_JS/script.js new file mode 100644 index 0000000..5b0e602 --- /dev/null +++ b/Traversy_Brad_Modern_JS/script.js @@ -0,0 +1,618 @@ +class Wallet { + #balance = 0; + #transactions = []; + + + constructor() { + this._balance = 0; + this._transactions = []; + } + + deposit(amount) { + this.#processDeposit(amount); + this.#balance += amount; + } + + withdraw(amount) { + if(amount >= this.#balance){ + console.log('Not enough funds'); + return; + } + this.#processWithdraw(amount); + this.#balance -= amount; + } + + #processDeposit(amount) { + console.log(`Depositng ${amount}`); + + this._transactions.push({ + type: 'deposit', + amount + }) + } + + #processWithdraw(amount) { + console.log(`Withdrawing ${amount}`); + + this._transactions.push({ + type: 'withdraw', + amount + }) + } + + get balance() { + return this.#balance; + } + + get transactions() { + return this.#transactions; + } +} + +const wallet = new Wallet(); +wallet.deposit(300); +wallet.withdraw(50); +// console.log(wallet._balance); +// console.log(wallet.balance); +// console.log(wallet.transactions); + +// console.log(wallet.#balance); +console.log(wallet.transactions); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// // Constructor function +// function Person (firstName, lastName){ +// this._firstName = firstName; +// this._lastName = lastName; + +// Object.defineProperty(this, 'firstName', { +// get: function () { +// return this.captializeFirst(this._firstName); +// }, +// set: function (value) { +// this._firstName = value; +// } +// }); + +// Object.defineProperty(this, 'lastName', { +// get: function () { +// return this.captializeFirst(this._lastName); +// }, +// set: function (value) { +// this._lasstName = value; +// } +// }); + +// Object.defineProperty(this, 'fullName', { +// get: function () { +// return this.firstName + " " + this.lastName; +// }, +// }); + +// } + +// Person.prototype.captializeFirst = function(value) { +// return value.charAt(0).toUpperCase() + value.slice(1); +// }; + +// //Object literal +// const PersonObj = { +// _firstName: 'jane', +// _lastName: 'doe', + +// get firstName() { +// return Person.prototype.captializeFirst(this._firstName) +// }, + +// set firstName(value) { +// this._firstName = value; +// }, + +// get lastName() { +// return Person.prototype.captializeFirst(this._lastName); +// }, + +// set lastName(value) { +// this._lastName = value; +// }, + +// get fullName() { +// return this._firstName + " " + this.lastName; +// } +// } + +// const person1 = new Person('john', 'doe'); +// console.log(person1.firstName); +// console.log(person1.lastName); +// console.log(person1.fullName); + +// const person2 = new Person('jane', 'dough'); +// console.log(person2.firstName); +// console.log(person2.lastName); +// console.log(person2.fullName); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// class Person { +// constructor (firstName, lastName) { +// this._firstName = firstName; +// this._lastName = lastName; +// } + +// get firstName() { +// // return this._firstName.charAt(0).toUpperCase() + this._firstName.slice(1); +// return this.captializeFirst(this._firstName); +// } + +// set firstName(value) { +// this._firstName = value.charAt(0).toUpperCase() + value.slice(1); +// } + +// get lastName() { +// // return this._firstName.charAt(0).toUpperCase() + this._firstName.slice(1); +// return this.captializeFirst(this._lastName); +// } + +// set lastName(value) { +// this._lastName = value.charAt(0).toUpperCase() + value.slice(1); +// } + +// captializeFirst(value) { +// return value.charAt(0).toUpperCase() + value.slice(1); +// } + +// get fullName() { +// return `${this.firstName} ${this._lastName}`; +// } +// } + + +// const person1 = new Person('john', 'doe'); +// console.log(person1.firstName); +// console.log(person1.lastName); + + +// person1.firstName = 'joseph'; +// person1.lastName = 'smith'; +// console.log(person1); +// console.log(person1.fullName); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// class App { +// constructor() { +// this.serverName = 'localhost'; + +// document.querySelector('button').addEventListener('Click', this.getServerName.bind(this)); +// } + +// getServerName() { +// console.log(this.serverName); +// } +// } + +// const app = new App(); +// // app.getServerName(); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// class Rectangle { +// constructor(name, height, width) { +// this.name = name; +// this.height = height; +// this.width = width; +// } + +// area() { +// return this.height * this.width; +// } + +// static getClass() { +// return 'Rectangle'; +// } +// } + + +// const rect = new Rectangle('Rect', 10, 10); +// console.log(rect.area()); +// console.log(Rectangle.getClass()); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// // Parent class +// class Shape { +// constructor(name) { +// this.name = name; +// } + +// logName() { +// console.log('Shape name: ', this.name); +// } +// } + +// // Sub Class +// class Rectangle extends Shape { +// constructor(name, width, height) { +// super(name); + +// this.height = height; +// this.width = width; +// } +// } + +// class Circle extends Shape { +// constructor(name, radius) { +// super(name); + +// this.readius = radius; +// } + +// logName() { +// console.log('Circle Name: ' + this.name) +// } +// } + + + + +// const rect = new Rectangle('Rect 1', 20, 20); +// console.log(rect); +// rect.logName(); + +// const cir = new Circle('Cir 1', 30); +// cir.logName(); + +// console.log(rect instanceof Rectangle); +// console.log(rect instanceof Shape); + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// function Player(name) { +// this.name = name; +// this.lvl = 1; +// this.points = 0; +// } + +// Player.prototype.gainXp = function (xp) { +// this.points += xp; + +// if(this.points >= 10) +// { this.lvl++; +// this.points -= 10; +// } +// } + +// Player.prototype.describe = function(){ +// return `${this.name} is level ${this.lvl} with ${this.points} +// experience points`; +// } + +// const player1 = new Player('Bob'); +// const player2 = new Player('Alice'); + +// player1.gainXp(4); +// player2.gainXp(7); +// player1.gainXp(5); +// player2.gainXp(1); +// player1.gainXp(7); +// player2.gainXp(9); +// player1.gainXp(5); +// player2.gainXp(2); + +// console.log(player1.describe()); +// console.log(player2.describe()); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// function Rectangle(name, width, height) { +// this.name = name; +// this.width = width; +// this.height = height; +// this.area = function() { +// return this.width * this.height; +// }; +// } + +// const rect = new Rectangle('React', 10, 10); +// console.log(rect); + +// Rectangle.prototype.area = function () { +// return (this.width * this.height); +// } + +// const rectanglePrototypes = { +// area: function () { +// return this.width * this.height +// }, +// perimeter: function () { +// return 2 * (this.width + this.height) +// }, +// isSquare: function () { +// return (this.height === this.width) +// } +// } + + +// function createRectangle(height, width) { +// return Object.create(rectanglePrototypes, { +// height: { +// value: height, +// }, +// width: { +// value: width, +// }, +// }); +// } + +// const rect = createRectangle(10, 20); +// console.log(rect); +// console.log(rect.area()); +// console.log(rect.isSquare()); + +// const rect2 = createRectangle(20, 20); +// console.log(rect2.area()); + + + + + + + + + + + + + + + + + + + + + + + +// const strLit = 'hello'; +// const strObj = new String('HELLO'); + +// console.log(strLit, typeof strLit); +// console.log(strObj, typeof strObj); + +// console.log(strLit.toUpperCase()); +// console.log(strLit[0]); + +// const funcLit = function(x) { +// return x * x; +// } + +// console.log(funcLit, typeof funcLit); + +// const funcObj = new Function('x', 'return x * x'); + +// console.log(funcObj(3)); + +// const obj1 = {}; +// const obj2 = new Object(); + +// console.log(obj1, typeof obj1); +// console.log(obj2, typeof obj2); +