From fefce72866fa35e0b24bde2c0fa6870f5243d500 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Sat, 17 Feb 2024 01:27:43 -0500 Subject: [PATCH 01/30] triple_Add curried function --- Traub-Eric-JS-Practice/iife.js | 0 Traub-Eric-JS-Practice/tripleAdd.js | 9 +++++++++ 2 files changed, 9 insertions(+) create mode 100644 Traub-Eric-JS-Practice/iife.js create mode 100644 Traub-Eric-JS-Practice/tripleAdd.js diff --git a/Traub-Eric-JS-Practice/iife.js b/Traub-Eric-JS-Practice/iife.js new file mode 100644 index 0000000..e69de29 diff --git a/Traub-Eric-JS-Practice/tripleAdd.js b/Traub-Eric-JS-Practice/tripleAdd.js new file mode 100644 index 0000000..73920b1 --- /dev/null +++ b/Traub-Eric-JS-Practice/tripleAdd.js @@ -0,0 +1,9 @@ +tripleAdd(10)(20)(30); // 60 + +function tripleAdd(num1) { + return function(num2) { + return function(num3) { + return num1 + num2 + num3; + } + } +} From 69e91aaca6bae53601487904327551b0dec5e37f Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Sat, 17 Feb 2024 01:39:04 -0500 Subject: [PATCH 02/30] implement 5 buttons solution w. iife to activate on each loop --- Traub-Eric-JS-Practice/button_5.js | 30 ++++++++++++++++++++++++++++++ Traub-Eric-JS-Practice/iife.js | 7 +++++++ 2 files changed, 37 insertions(+) create mode 100644 Traub-Eric-JS-Practice/button_5.js diff --git a/Traub-Eric-JS-Practice/button_5.js b/Traub-Eric-JS-Practice/button_5.js new file mode 100644 index 0000000..0f33a05 --- /dev/null +++ b/Traub-Eric-JS-Practice/button_5.js @@ -0,0 +1,30 @@ +// 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(); + + + 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) + body.appendChild(button); + } + } + + createButtons(); \ No newline at end of file diff --git a/Traub-Eric-JS-Practice/iife.js b/Traub-Eric-JS-Practice/iife.js index e69de29..356033e 100644 --- a/Traub-Eric-JS-Practice/iife.js +++ b/Traub-Eric-JS-Practice/iife.js @@ -0,0 +1,7 @@ +// immediately invoked function + +(function doubleNumber(num){ + return num * 2; +})(10); + +doubleNumber(5); \ No newline at end of file From ffff49aa579fb4868a832e733c3d2f489f0e4f55 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Sat, 17 Feb 2024 02:16:06 -0500 Subject: [PATCH 03/30] button_5 external addClick solution, and let solution: block scoped variable, not function scoped --- Traub-Eric-JS-Practice/button_5.js | 36 +++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/Traub-Eric-JS-Practice/button_5.js b/Traub-Eric-JS-Practice/button_5.js index 0f33a05..4904551 100644 --- a/Traub-Eric-JS-Practice/button_5.js +++ b/Traub-Eric-JS-Practice/button_5.js @@ -12,17 +12,43 @@ // createButtons(); + // Solution 2: - function createButtons() { +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) { + // (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 ' + num); - }; - })(i) + alert('This is button ' + i); + } body.appendChild(button); } } From 95e484f0324fe3aa1f29948cbdfa76c727d8a16f Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Sat, 17 Feb 2024 03:36:13 -0500 Subject: [PATCH 04/30] closure example: -access to local vars, -access to outer func vars, -access to global vars, -access to local, outer params --- Traub-Eric-JS-Practice/closure.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Traub-Eric-JS-Practice/closure.js diff --git a/Traub-Eric-JS-Practice/closure.js b/Traub-Eric-JS-Practice/closure.js new file mode 100644 index 0000000..e69de29 From dcf0225dc1603cf3f2b3384d9b6319715b7ea2ee Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Sat, 17 Feb 2024 03:49:49 -0500 Subject: [PATCH 05/30] this keyword: global window object, or -locally scoped object reference --- Traub-Eric-JS-Practice/this.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Traub-Eric-JS-Practice/this.js diff --git a/Traub-Eric-JS-Practice/this.js b/Traub-Eric-JS-Practice/this.js new file mode 100644 index 0000000..e69de29 From f100b22eea6cc7aeb33655a27c1b739502b89af4 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Sat, 17 Feb 2024 11:34:50 -0500 Subject: [PATCH 06/30] hoisting: -variables are hoisted but not initialized until declaration -function expression vs function declaration -hoisting variables only occur within encompassing function -keywords let, const not initialized until they are declared --- Traub-Eric-JS-Practice/{tripleAdd.js => 1_tripleAdd.js} | 0 Traub-Eric-JS-Practice/{iife.js => 2_iife.js} | 0 Traub-Eric-JS-Practice/{button_5.js => 3_button_5.js} | 0 Traub-Eric-JS-Practice/{closure.js => 4_closure.js} | 0 Traub-Eric-JS-Practice/{this.js => 5_this.js} | 0 Traub-Eric-JS-Practice/6_hoisting.js | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename Traub-Eric-JS-Practice/{tripleAdd.js => 1_tripleAdd.js} (100%) rename Traub-Eric-JS-Practice/{iife.js => 2_iife.js} (100%) rename Traub-Eric-JS-Practice/{button_5.js => 3_button_5.js} (100%) rename Traub-Eric-JS-Practice/{closure.js => 4_closure.js} (100%) rename Traub-Eric-JS-Practice/{this.js => 5_this.js} (100%) create mode 100644 Traub-Eric-JS-Practice/6_hoisting.js diff --git a/Traub-Eric-JS-Practice/tripleAdd.js b/Traub-Eric-JS-Practice/1_tripleAdd.js similarity index 100% rename from Traub-Eric-JS-Practice/tripleAdd.js rename to Traub-Eric-JS-Practice/1_tripleAdd.js diff --git a/Traub-Eric-JS-Practice/iife.js b/Traub-Eric-JS-Practice/2_iife.js similarity index 100% rename from Traub-Eric-JS-Practice/iife.js rename to Traub-Eric-JS-Practice/2_iife.js diff --git a/Traub-Eric-JS-Practice/button_5.js b/Traub-Eric-JS-Practice/3_button_5.js similarity index 100% rename from Traub-Eric-JS-Practice/button_5.js rename to Traub-Eric-JS-Practice/3_button_5.js diff --git a/Traub-Eric-JS-Practice/closure.js b/Traub-Eric-JS-Practice/4_closure.js similarity index 100% rename from Traub-Eric-JS-Practice/closure.js rename to Traub-Eric-JS-Practice/4_closure.js diff --git a/Traub-Eric-JS-Practice/this.js b/Traub-Eric-JS-Practice/5_this.js similarity index 100% rename from Traub-Eric-JS-Practice/this.js rename to Traub-Eric-JS-Practice/5_this.js diff --git a/Traub-Eric-JS-Practice/6_hoisting.js b/Traub-Eric-JS-Practice/6_hoisting.js new file mode 100644 index 0000000..e69de29 From 7a32f42fdc5b26033f218baac4d38c02a49e3d92 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Sat, 17 Feb 2024 11:38:21 -0500 Subject: [PATCH 07/30] self: -inner this undefined at iife -inner self refers to outer self through closure --- Traub-Eric-JS-Practice/7_self.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Traub-Eric-JS-Practice/7_self.js 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 From bc1b664619eaf6b9c5d180e3a85e7aa87f7f5e43 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Sat, 17 Feb 2024 11:43:48 -0500 Subject: [PATCH 08/30] double equals type converts second var to first --- Traub-Eric-JS-Practice/8_equals_vs_strict.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Traub-Eric-JS-Practice/8_equals_vs_strict.js 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 From df021092e38cfeaed7b17ffc696d9aec7f176479 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Sat, 17 Feb 2024 11:47:10 -0500 Subject: [PATCH 09/30] var hoisting within functions prevents access prior to assignment --- Traub-Eric-JS-Practice/9_log_numer | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Traub-Eric-JS-Practice/9_log_numer diff --git a/Traub-Eric-JS-Practice/9_log_numer b/Traub-Eric-JS-Practice/9_log_numer new file mode 100644 index 0000000..e69de29 From 846f72552816cdc18b8b1026bfb7d134bc57fae7 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Sat, 17 Feb 2024 11:59:36 -0500 Subject: [PATCH 10/30] use strict: fail fast && fail loudly: -errors thrown for improper var declations -params must be unique -unable to delete bult-in properties --- Traub-Eric-JS-Practice/10_use_strict.js | 44 +++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Traub-Eric-JS-Practice/10_use_strict.js 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 From 981ceab3d9c4d20cb21dd7a1fea8d957880893ad Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Mon, 19 Feb 2024 19:01:01 -0500 Subject: [PATCH 11/30] curry func: -getRavelTime pass in distance -return function: pass in speed -return distanc divided by speed -create travelTimeBosNyc: pass in distance to getTravelTime -call travelTimeBosNyc w. various speeds to get hours --- Traub-Eric-JS-Practice/11_curry_function | 0 Traub-Eric-JS-Practice/9_log_numer | 8 ++++++++ 2 files changed, 8 insertions(+) create mode 100644 Traub-Eric-JS-Practice/11_curry_function diff --git a/Traub-Eric-JS-Practice/11_curry_function b/Traub-Eric-JS-Practice/11_curry_function new file mode 100644 index 0000000..e69de29 diff --git a/Traub-Eric-JS-Practice/9_log_numer b/Traub-Eric-JS-Practice/9_log_numer index e69de29..016e719 100644 --- a/Traub-Eric-JS-Practice/9_log_numer +++ b/Traub-Eric-JS-Practice/9_log_numer @@ -0,0 +1,8 @@ +var num = 50; + +function logNumber() { + console.log(num); + var num = 100; +} + +logNumber(); \ No newline at end of file From 33398357f8883c6b7dfe6e6fda6b146012e20955 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Mon, 19 Feb 2024 19:07:50 -0500 Subject: [PATCH 12/30] counter function: -set count to zero -return function -increment count -return count -create instanceOne equal to myFunc -create instanceTwo equal to myFunc -throw error if func called more than x times --- Traub-Eric-JS-Practice/12_counter_function | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Traub-Eric-JS-Practice/12_counter_function diff --git a/Traub-Eric-JS-Practice/12_counter_function b/Traub-Eric-JS-Practice/12_counter_function new file mode 100644 index 0000000..c672cd3 --- /dev/null +++ b/Traub-Eric-JS-Practice/12_counter_function @@ -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 From 833cda7707c3df89dbc2fca6d5f2d59678e111c8 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Mon, 19 Feb 2024 19:14:09 -0500 Subject: [PATCH 13/30] y is declared globally without var var x is equal to y but not defined -user strict avoids this issue --- Traub-Eric-JS-Practice/13_logging_x_and_y | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Traub-Eric-JS-Practice/13_logging_x_and_y diff --git a/Traub-Eric-JS-Practice/13_logging_x_and_y b/Traub-Eric-JS-Practice/13_logging_x_and_y new file mode 100644 index 0000000..e69de29 From 83800452a87ffdfafe1ccfa3270eb3780812911a Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Tue, 20 Feb 2024 20:18:18 -0500 Subject: [PATCH 14/30] object oriented game example --- Traversy_Brad_Modern_JS/objects.js | 159 +++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 Traversy_Brad_Modern_JS/objects.js diff --git a/Traversy_Brad_Modern_JS/objects.js b/Traversy_Brad_Modern_JS/objects.js new file mode 100644 index 0000000..70bf9ed --- /dev/null +++ b/Traversy_Brad_Modern_JS/objects.js @@ -0,0 +1,159 @@ +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); + From 64e8ce6fb32a25457bc91760699376c6b45cfc96 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Tue, 20 Feb 2024 23:08:18 -0500 Subject: [PATCH 15/30] class inheritance w. polymorphism example --- Traversy_Brad_Modern_JS/index.html | 12 ++ .../{objects.js => script.js} | 121 ++++++++++++++---- 2 files changed, 109 insertions(+), 24 deletions(-) create mode 100644 Traversy_Brad_Modern_JS/index.html rename Traversy_Brad_Modern_JS/{objects.js => script.js} (53%) diff --git a/Traversy_Brad_Modern_JS/index.html b/Traversy_Brad_Modern_JS/index.html new file mode 100644 index 0000000..8f0c482 --- /dev/null +++ b/Traversy_Brad_Modern_JS/index.html @@ -0,0 +1,12 @@ + + + + + + Document + + +

Objects

+ + + \ No newline at end of file diff --git a/Traversy_Brad_Modern_JS/objects.js b/Traversy_Brad_Modern_JS/script.js similarity index 53% rename from Traversy_Brad_Modern_JS/objects.js rename to Traversy_Brad_Modern_JS/script.js index 70bf9ed..097d2b3 100644 --- a/Traversy_Brad_Modern_JS/objects.js +++ b/Traversy_Brad_Modern_JS/script.js @@ -1,37 +1,110 @@ -function Player(name) { - this.name = name; - this.lvl = 1; - this.points = 0; +// Parent class +class Shape { + constructor(name) { + this.name = name; + } + + logName() { + console.log('Shape name: ', this.name); + } } -Player.prototype.gainXp = function (xp) { - this.points += xp; +// Sub Class +class Rectangle extends Shape { + constructor(name, width, height) { + super(name); - if(this.points >= 10) - { this.lvl++; - this.points -= 10; + this.height = height; + this.width = width; } } -Player.prototype.describe = function(){ - return `${this.name} is level ${this.lvl} with ${this.points} - experience points`; +class Circle extends Shape { + constructor(name, radius) { + super(name); + + this.readius = radius; + } + + logName() { + console.log('Circle Name: ' + this.name) + } } -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()); + +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()); From 72860ef08d1f124f0e671262cd100c392f18851d Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Tue, 20 Feb 2024 23:11:38 -0500 Subject: [PATCH 16/30] static method example --- Traversy_Brad_Modern_JS/script.js | 121 +++++++++++++++++++++++------- 1 file changed, 92 insertions(+), 29 deletions(-) diff --git a/Traversy_Brad_Modern_JS/script.js b/Traversy_Brad_Modern_JS/script.js index 097d2b3..a2f0f75 100644 --- a/Traversy_Brad_Modern_JS/script.js +++ b/Traversy_Brad_Modern_JS/script.js @@ -1,48 +1,111 @@ -// Parent class -class Shape { - constructor(name) { +class Rectangle { + constructor(name, height, width) { this.name = name; + this.height = height; + this.width = width; } - logName() { - console.log('Shape name: ', this.name); + area() { + return this.height * this.width; } -} -// Sub Class -class Rectangle extends Shape { - constructor(name, width, height) { - super(name); - - this.height = height; - this.width = width; + static getClass() { + return 'Rectangle'; } } -class Circle extends Shape { - constructor(name, radius) { - super(name); - this.readius = radius; - } +const rect = new Rectangle('Rect', 10, 10); +console.log(rect.area()); +console.log(Rectangle.getClass()); + + + + + + + + + + + + + + + + + + + + + + + + - 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); + + + + + + + + + + + + +// // 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); From 33cdfeeaf2b868bdd3d55accf0af17a6dd31f239 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Tue, 20 Feb 2024 23:21:28 -0500 Subject: [PATCH 17/30] bind(this) example with click button and eventListener --- Traversy_Brad_Modern_JS/index.html | 1 + Traversy_Brad_Modern_JS/script.js | 76 +++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 13 deletions(-) diff --git a/Traversy_Brad_Modern_JS/index.html b/Traversy_Brad_Modern_JS/index.html index 8f0c482..f15b337 100644 --- a/Traversy_Brad_Modern_JS/index.html +++ b/Traversy_Brad_Modern_JS/index.html @@ -8,5 +8,6 @@

Objects

+ \ No newline at end of file diff --git a/Traversy_Brad_Modern_JS/script.js b/Traversy_Brad_Modern_JS/script.js index a2f0f75..2e34ec4 100644 --- a/Traversy_Brad_Modern_JS/script.js +++ b/Traversy_Brad_Modern_JS/script.js @@ -1,23 +1,73 @@ -class Rectangle { - constructor(name, height, width) { - this.name = name; - this.height = height; - this.width = width; - } +class App { + constructor() { + this.serverName = 'localhost'; - area() { - return this.height * this.width; + document.querySelector('button').addEventListener('Click', this.getServerName.bind(this)); } - static getClass() { - return 'Rectangle'; + getServerName() { + console.log(this.serverName); } } + +const app = new App(); +// app.getServerName(); + + + + + + + + + + + + + + + + + + + + + + -const rect = new Rectangle('Rect', 10, 10); -console.log(rect.area()); -console.log(Rectangle.getClass()); + + + + + + + + + + + + +// 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()); From 0709cd4dd3587f9eef5a6c41405639e4f8d82006 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Tue, 20 Feb 2024 23:37:11 -0500 Subject: [PATCH 18/30] getters and setters w. classes --- Traversy_Brad_Modern_JS/script.js | 92 ++++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 8 deletions(-) diff --git a/Traversy_Brad_Modern_JS/script.js b/Traversy_Brad_Modern_JS/script.js index 2e34ec4..cd753a8 100644 --- a/Traversy_Brad_Modern_JS/script.js +++ b/Traversy_Brad_Modern_JS/script.js @@ -1,17 +1,93 @@ -class App { - constructor() { - this.serverName = 'localhost'; +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); + } - document.querySelector('button').addEventListener('Click', this.getServerName.bind(this)); + set lastName(value) { + this._lastName = value.charAt(0).toUpperCase() + value.slice(1); } - getServerName() { - console.log(this.serverName); + captializeFirst(value) { + return value.charAt(0).toUpperCase() + value.slice(1); + } + + get fullName() { + return `${this.firstName} ${this._lastName}`; } } -const app = new App(); -// app.getServerName(); + +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(); From bb165246f8f7d4add31bc7fc3b956f243cb5abab Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Wed, 21 Feb 2024 22:50:57 -0500 Subject: [PATCH 19/30] geters and setters w. define property --- Traversy_Brad_Modern_JS/script.js | 151 +++++++++++++++++++++++++----- 1 file changed, 125 insertions(+), 26 deletions(-) diff --git a/Traversy_Brad_Modern_JS/script.js b/Traversy_Brad_Modern_JS/script.js index cd753a8..b7b03ea 100644 --- a/Traversy_Brad_Modern_JS/script.js +++ b/Traversy_Brad_Modern_JS/script.js @@ -1,46 +1,145 @@ -class Person { - constructor (firstName, lastName) { - this._firstName = firstName; - this._lastName = lastName; - } +// 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 this._firstName.charAt(0).toUpperCase() + this._firstName.slice(1); - return this.captializeFirst(this._firstName); - } + return Person.prototype.captializeFirst(this._firstName) + }, set firstName(value) { - this._firstName = value.charAt(0).toUpperCase() + value.slice(1); - } + this._firstName = value; + }, get lastName() { - // return this._firstName.charAt(0).toUpperCase() + this._firstName.slice(1); - return this.captializeFirst(this._lastName); - } + return Person.prototype.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); - } - + this._lastName = value; + }, + get fullName() { - return `${this.firstName} ${this._lastName}`; + 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); -person1.firstName = 'joseph'; -person1.lastName = 'smith'; -console.log(person1); -console.log(person1.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); From a98bab0d018cad0ac404f8534737aaeb52547f88 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Wed, 21 Feb 2024 23:08:54 -0500 Subject: [PATCH 20/30] private property convention w. getters and setters --- Traversy_Brad_Modern_JS/script.js | 205 +++++++++++++++++++++--------- 1 file changed, 148 insertions(+), 57 deletions(-) diff --git a/Traversy_Brad_Modern_JS/script.js b/Traversy_Brad_Modern_JS/script.js index b7b03ea..a3fce5f 100644 --- a/Traversy_Brad_Modern_JS/script.js +++ b/Traversy_Brad_Modern_JS/script.js @@ -1,73 +1,164 @@ -// 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; +class Wallet { + 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; + } - Object.defineProperty(this, 'fullName', { - get: function () { - return this.firstName + " " + this.lastName; - }, - }); + _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; + } } -Person.prototype.captializeFirst = function(value) { - return value.charAt(0).toUpperCase() + value.slice(1); -}; +const wallet = new Wallet(); +wallet.deposit(300); +wallet.withdraw(50); +// console.log(wallet._balance); +console.log(wallet.balance); +console.log(wallet.transactions); + + + + + + + + + + + + + + + + + + + + + + + + + + + -//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; - }, + + + + + + +// // 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; - } -} +// 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 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); +// const person2 = new Person('jane', 'dough'); +// console.log(person2.firstName); +// console.log(person2.lastName); +// console.log(person2.fullName); From 55078c11d44191d41648ff76bb8b9e349db71e0b Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Thu, 22 Feb 2024 08:09:08 -0500 Subject: [PATCH 21/30] private class fields example --- Traversy_Brad_Modern_JS/script.js | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/Traversy_Brad_Modern_JS/script.js b/Traversy_Brad_Modern_JS/script.js index a3fce5f..5b0e602 100644 --- a/Traversy_Brad_Modern_JS/script.js +++ b/Traversy_Brad_Modern_JS/script.js @@ -1,24 +1,28 @@ class Wallet { + #balance = 0; + #transactions = []; + + constructor() { this._balance = 0; this._transactions = []; } deposit(amount) { - this._processDeposit(amount); - this._balance += amount; + this.#processDeposit(amount); + this.#balance += amount; } withdraw(amount) { - if(amount >= this._balance){ + if(amount >= this.#balance){ console.log('Not enough funds'); return; } - this._processWithdraw(amount); - this._balance -= amount; + this.#processWithdraw(amount); + this.#balance -= amount; } - _processDeposit(amount) { + #processDeposit(amount) { console.log(`Depositng ${amount}`); this._transactions.push({ @@ -27,7 +31,7 @@ class Wallet { }) } - _processWithdraw(amount) { + #processWithdraw(amount) { console.log(`Withdrawing ${amount}`); this._transactions.push({ @@ -37,11 +41,11 @@ class Wallet { } get balance() { - return this._balance; + return this.#balance; } get transactions() { - return this._transactions; + return this.#transactions; } } @@ -49,7 +53,10 @@ const wallet = new Wallet(); wallet.deposit(300); wallet.withdraw(50); // console.log(wallet._balance); -console.log(wallet.balance); +// console.log(wallet.balance); +// console.log(wallet.transactions); + +// console.log(wallet.#balance); console.log(wallet.transactions); From 8b44420c13d843abecd65ad3fa14cd29f56d07cb Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Wed, 20 Mar 2024 09:52:22 -0400 Subject: [PATCH 22/30] call(): func that allows change of this function and execution w. args provided apply(): pass all params as array to obj passed in --- Traub-Eric-JS-Practice/14_call_and_apply | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Traub-Eric-JS-Practice/14_call_and_apply diff --git a/Traub-Eric-JS-Practice/14_call_and_apply b/Traub-Eric-JS-Practice/14_call_and_apply new file mode 100644 index 0000000..2db210d --- /dev/null +++ b/Traub-Eric-JS-Practice/14_call_and_apply @@ -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 From 258bba6e8275505d855b8904b43311af42083f81 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Wed, 20 Mar 2024 10:18:08 -0400 Subject: [PATCH 23/30] pass by reference example --- Traub-Eric-JS-Practice/15_determine_list2 | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Traub-Eric-JS-Practice/15_determine_list2 diff --git a/Traub-Eric-JS-Practice/15_determine_list2 b/Traub-Eric-JS-Practice/15_determine_list2 new file mode 100644 index 0000000..dbe3a31 --- /dev/null +++ b/Traub-Eric-JS-Practice/15_determine_list2 @@ -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 From dcb5cc922ecee4f3176bca1d1f876939231590a4 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Wed, 20 Mar 2024 10:24:22 -0400 Subject: [PATCH 24/30] doubly invoked function example: getTotal() var args = Array.prototype.slice.call(arguments) if(args.len === 2) rt args[0] + args[1] else if (args.len === 1) rt func(num2) rt args[0] + num2 --- .../16_singly_doubly_invoked_functions | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Traub-Eric-JS-Practice/16_singly_doubly_invoked_functions diff --git a/Traub-Eric-JS-Practice/16_singly_doubly_invoked_functions b/Traub-Eric-JS-Practice/16_singly_doubly_invoked_functions new file mode 100644 index 0000000..901eeaa --- /dev/null +++ b/Traub-Eric-JS-Practice/16_singly_doubly_invoked_functions @@ -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 From a6a545387a32e90d95fe3e6f3676943ee8d4feb5 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Wed, 20 Mar 2024 10:32:18 -0400 Subject: [PATCH 25/30] json data example --- Traub-Eric-JS-Practice/11_curry_function | 0 Traub-Eric-JS-Practice/11_curry_function.js | 32 +++++++++++++++++++ ...ounter_function => 12_counter_function.js} | 0 Traub-Eric-JS-Practice/13_logging_x_and_y | 0 Traub-Eric-JS-Practice/13_logging_x_and_y.js | 8 +++++ ...14_call_and_apply => 14_call_and_apply.js} | 0 ..._determine_list2 => 15_determine_list2.js} | 0 ... => 16_singly_doubly_invoked_functions.js} | 0 Traub-Eric-JS-Practice/17_json_data.js | 17 ++++++++++ .../{9_log_numer => 9_log_numer.js} | 0 10 files changed, 57 insertions(+) delete mode 100644 Traub-Eric-JS-Practice/11_curry_function create mode 100644 Traub-Eric-JS-Practice/11_curry_function.js rename Traub-Eric-JS-Practice/{12_counter_function => 12_counter_function.js} (100%) delete mode 100644 Traub-Eric-JS-Practice/13_logging_x_and_y create mode 100644 Traub-Eric-JS-Practice/13_logging_x_and_y.js rename Traub-Eric-JS-Practice/{14_call_and_apply => 14_call_and_apply.js} (100%) rename Traub-Eric-JS-Practice/{15_determine_list2 => 15_determine_list2.js} (100%) rename Traub-Eric-JS-Practice/{16_singly_doubly_invoked_functions => 16_singly_doubly_invoked_functions.js} (100%) create mode 100644 Traub-Eric-JS-Practice/17_json_data.js rename Traub-Eric-JS-Practice/{9_log_numer => 9_log_numer.js} (100%) diff --git a/Traub-Eric-JS-Practice/11_curry_function b/Traub-Eric-JS-Practice/11_curry_function deleted file mode 100644 index e69de29..0000000 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 b/Traub-Eric-JS-Practice/12_counter_function.js similarity index 100% rename from Traub-Eric-JS-Practice/12_counter_function rename to Traub-Eric-JS-Practice/12_counter_function.js diff --git a/Traub-Eric-JS-Practice/13_logging_x_and_y b/Traub-Eric-JS-Practice/13_logging_x_and_y deleted file mode 100644 index e69de29..0000000 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 b/Traub-Eric-JS-Practice/14_call_and_apply.js similarity index 100% rename from Traub-Eric-JS-Practice/14_call_and_apply rename to Traub-Eric-JS-Practice/14_call_and_apply.js diff --git a/Traub-Eric-JS-Practice/15_determine_list2 b/Traub-Eric-JS-Practice/15_determine_list2.js similarity index 100% rename from Traub-Eric-JS-Practice/15_determine_list2 rename to Traub-Eric-JS-Practice/15_determine_list2.js diff --git a/Traub-Eric-JS-Practice/16_singly_doubly_invoked_functions b/Traub-Eric-JS-Practice/16_singly_doubly_invoked_functions.js similarity index 100% rename from Traub-Eric-JS-Practice/16_singly_doubly_invoked_functions rename to Traub-Eric-JS-Practice/16_singly_doubly_invoked_functions.js 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/9_log_numer b/Traub-Eric-JS-Practice/9_log_numer.js similarity index 100% rename from Traub-Eric-JS-Practice/9_log_numer rename to Traub-Eric-JS-Practice/9_log_numer.js From e60b89b785cb8436805c2f6ae6b85057a53d52f1 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Wed, 20 Mar 2024 10:36:51 -0400 Subject: [PATCH 26/30] setTimeout output in event loop example --- Traub-Eric-JS-Practice/18_order_logged_out | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Traub-Eric-JS-Practice/18_order_logged_out diff --git a/Traub-Eric-JS-Practice/18_order_logged_out b/Traub-Eric-JS-Practice/18_order_logged_out new file mode 100644 index 0000000..2666ce6 --- /dev/null +++ b/Traub-Eric-JS-Practice/18_order_logged_out @@ -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 From ddb6750de182f22d71dd24a11a7e2c3cd32fc226 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Wed, 20 Mar 2024 12:45:34 -0400 Subject: [PATCH 27/30] object construction: -object literal syntaz const obj ={} -new keyword and object constrcutor: const student = new Object() -constructor function: function Car(color, brand, year) --- ...rder_logged_out => 18_order_logged_out.js} | 0 Traub-Eric-JS-Practice/19_creating_objects.js | 38 +++++++++++++++++++ 2 files changed, 38 insertions(+) rename Traub-Eric-JS-Practice/{18_order_logged_out => 18_order_logged_out.js} (100%) create mode 100644 Traub-Eric-JS-Practice/19_creating_objects.js diff --git a/Traub-Eric-JS-Practice/18_order_logged_out b/Traub-Eric-JS-Practice/18_order_logged_out.js similarity index 100% rename from Traub-Eric-JS-Practice/18_order_logged_out rename to Traub-Eric-JS-Practice/18_order_logged_out.js 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 From d562ad2eb154fac97ac6ee921062583cb7ddc030 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Wed, 20 Mar 2024 12:51:07 -0400 Subject: [PATCH 28/30] data type examples --- Traub-Eric-JS-Practice/20_typeof_data_types.js | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 Traub-Eric-JS-Practice/20_typeof_data_types.js 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 From 7fe0728e599343dce02118747805a370f7411240 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Wed, 20 Mar 2024 13:05:06 -0400 Subject: [PATCH 29/30] palindrome solution w. double pointers: clean string w. .toLowerCase().replace(/[^\w]/g. ) set left, right pointer while left less than right if s[left] !== s[right]: return false increment left, decrement right, return true --- Nguyen-Kevin-JS-Leet-Practice/palindrome.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Nguyen-Kevin-JS-Leet-Practice/palindrome.js 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; +} + From b664521e0903b2eb8ac2f13e1c19f9728c544002 Mon Sep 17 00:00:00 2001 From: possibilitiesInTest Date: Thu, 21 Mar 2024 23:39:28 -0400 Subject: [PATCH 30/30] longestSubstring: -create empty hashMap for char|index -init start, max to zero iterate through input string check if curr Char in has and has index >= start set start to index for char found in hashmap + 1 set key|val on has to curr char | curr index if len of curr window greater than max set max to len of curr window return max len --- .../longestSubstring.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Nguyen-Kevin-JS-Leet-Practice/longestSubstring.js 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