From ec60e7239bbebff5e0a27deb74b8014d9237dbbd Mon Sep 17 00:00:00 2001 From: James Choi Date: Mon, 8 May 2017 21:20:02 -0700 Subject: [PATCH 1/7] stage1 --- bike-shop/src/stage1-literals.js | 16 ++++++++++++++++ jsinfo/Hello_object.js | 5 +++++ jsinfo/Multiply_by_2.js | 7 +++++++ jsinfo/chaining.js | 15 +++++++++++++++ jsinfo/createCalc.js | 21 +++++++++++++++++++++ 5 files changed, 64 insertions(+) create mode 100644 jsinfo/Hello_object.js create mode 100644 jsinfo/Multiply_by_2.js create mode 100644 jsinfo/chaining.js create mode 100644 jsinfo/createCalc.js diff --git a/bike-shop/src/stage1-literals.js b/bike-shop/src/stage1-literals.js index c749dd2..1ffb41d 100644 --- a/bike-shop/src/stage1-literals.js +++ b/bike-shop/src/stage1-literals.js @@ -1,5 +1,21 @@ const myBike = { // your code here + name: "Roadster", + price: 199.99, + frame: { + height: 55, + color: "blue", + style: "cruiser", + }, + brakes: { + back: true, + front: false, + }, + tires: { + diameter: 22, + type: "fat", + }, + rings: [2,5], } module.exports = myBike diff --git a/jsinfo/Hello_object.js b/jsinfo/Hello_object.js new file mode 100644 index 0000000..478f035 --- /dev/null +++ b/jsinfo/Hello_object.js @@ -0,0 +1,5 @@ +let user = {} +user.name = "John"; +user.surname = "Smith"; +user.name = "Pete"; +delete user.name; diff --git a/jsinfo/Multiply_by_2.js b/jsinfo/Multiply_by_2.js new file mode 100644 index 0000000..db05ad7 --- /dev/null +++ b/jsinfo/Multiply_by_2.js @@ -0,0 +1,7 @@ +function multiplyNumeric(obj) { + for (let key in obj) { + if (typeof obj[key] == 'number') { + obj[key] *= 2; + } + } + } diff --git a/jsinfo/chaining.js b/jsinfo/chaining.js new file mode 100644 index 0000000..18cdc7f --- /dev/null +++ b/jsinfo/chaining.js @@ -0,0 +1,15 @@ +let ladder = { + step: 0, + up() { + this.step++; + return this; + }, + down() { + this.step--; + return this; + }, + showStep: function() { + alert(this.step); + return this; + } +}; diff --git a/jsinfo/createCalc.js b/jsinfo/createCalc.js new file mode 100644 index 0000000..08df70a --- /dev/null +++ b/jsinfo/createCalc.js @@ -0,0 +1,21 @@ +let calculator = { + a; + b; + read() { + this.a = +prompt("number?"); + this.b = +prompt("2nd number?"); + + }, + + sum() { + return this.a + this.b; + + }, + + mul() { + return this.a * this.b; + + + } + +}; From a8fa305309b4920a0579132cda24e4c6d599653d Mon Sep 17 00:00:00 2001 From: James Choi Date: Tue, 9 May 2017 11:24:49 -0700 Subject: [PATCH 2/7] stage2 finished --- bike-shop/src/stage2-constructors.js | 23 +++++++++++++++++------ jsinfo/extendCalc.js | 22 ++++++++++++++++++++++ jsinfo/newAccu.js | 7 +++++++ jsinfo/newCalc.js | 17 +++++++++++++++++ npm-debug.log | 23 +++++++++++++++++++++++ 5 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 jsinfo/extendCalc.js create mode 100644 jsinfo/newAccu.js create mode 100644 jsinfo/newCalc.js create mode 100644 npm-debug.log diff --git a/bike-shop/src/stage2-constructors.js b/bike-shop/src/stage2-constructors.js index c422386..5c2cda5 100644 --- a/bike-shop/src/stage2-constructors.js +++ b/bike-shop/src/stage2-constructors.js @@ -1,13 +1,24 @@ -function Bike() { - // your code here +function Bike(name, price) { + this.name= name; + this.price= price; + this.frame = new Frame(); + this.rings = [3, 7]; + this.brakes = { + front: true, + back: true, + }; + this.tires = [new Tire(), new Tire()]; } -function Frame() { - // your code here +function Frame(color = 'black', size = 55, style = 'street') { + this.color = color; + this.size = size; + this.style = style; } -function Tire() { - // your code here +function Tire(diameter = 22, type = 'street') { +this.diameter = diameter; +this.type = type; } module.exports = { diff --git a/jsinfo/extendCalc.js b/jsinfo/extendCalc.js new file mode 100644 index 0000000..34766b1 --- /dev/null +++ b/jsinfo/extendCalc.js @@ -0,0 +1,22 @@ +function Calculator() { + + let methods = { + "-": (a, b) => a - b, + "+": (a, b) => a + b, + }; + + this.calculate = function(str) { + + let splitted = str.split(' '), + a = +splitted[0], + operator = splitted[1], + b = +splitted[2] + + return methods[operator](a, b); + } + + + this.addMethod = function(name, func) { + methods[name] = func; + }; +} diff --git a/jsinfo/newAccu.js b/jsinfo/newAccu.js new file mode 100644 index 0000000..d475410 --- /dev/null +++ b/jsinfo/newAccu.js @@ -0,0 +1,7 @@ +function Accumulator(startingValue) { + this.value = startingValue; + + this.read = function() { + this.value += prompt("number!"); + }; +} diff --git a/jsinfo/newCalc.js b/jsinfo/newCalc.js new file mode 100644 index 0000000..114f1f7 --- /dev/null +++ b/jsinfo/newCalc.js @@ -0,0 +1,17 @@ +function Calculator() { + this.read() = function() { + this.a = +prompt("value 1?") + this.b = +prompt("value 2?") + }, + + this.sum() = function() { + return this.a + this.b; + }, + + this.mul() = function(){ + return this.a * this.b; + }; + +} + +let calculator = new Calculator(); diff --git a/npm-debug.log b/npm-debug.log new file mode 100644 index 0000000..d31a984 --- /dev/null +++ b/npm-debug.log @@ -0,0 +1,23 @@ +0 info it worked if it ends with ok +1 verbose cli [ '/usr/local/bin/node', +1 verbose cli '/usr/local/bin/npm', +1 verbose cli 'run', +1 verbose cli 'test:stage2' ] +2 info using npm@3.10.10 +3 info using node@v6.10.3 +4 verbose stack Error: ENOENT: no such file or directory, open '/Users/jameschoi/LG/package.json' +4 verbose stack at Error (native) +5 verbose cwd /Users/jameschoi/LG/oop-practice +6 error Darwin 16.5.0 +7 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "test:stage2" +8 error node v6.10.3 +9 error npm v3.10.10 +10 error path /Users/jameschoi/LG/package.json +11 error code ENOENT +12 error errno -2 +13 error syscall open +14 error enoent ENOENT: no such file or directory, open '/Users/jameschoi/LG/package.json' +15 error enoent ENOENT: no such file or directory, open '/Users/jameschoi/LG/package.json' +15 error enoent This is most likely not a problem with npm itself +15 error enoent and is related to npm not being able to find a file. +16 verbose exit [ -2, true ] From 60ad923ea1cc35116f74cefe88cae332bb0968e6 Mon Sep 17 00:00:00 2001 From: James Choi Date: Thu, 11 May 2017 10:06:34 -0700 Subject: [PATCH 3/7] stage3 complete --- bike-shop/src/stage3-methods.js | 37 +++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/bike-shop/src/stage3-methods.js b/bike-shop/src/stage3-methods.js index c422386..a32ac4a 100644 --- a/bike-shop/src/stage3-methods.js +++ b/bike-shop/src/stage3-methods.js @@ -1,13 +1,46 @@ function Bike() { - // your code here + this._isMoving = false; + this.rings = [3,7]; + this.tires = [new Tire(), new Tire()]; +} + +Bike.prototype.isMoving = function () { + return this._isMoving; +} +Bike.prototype.pedal = function() { + this._isMoving = true; + if (this.tires[0]._isFlat || this.tires[1]._isFlat) { + throw("Can't pedal with a flat tire!"); + } + +Bike.prototype.brake = function() { + this._isMoving = false; +} + +Bike.prototype.gearSpeeds = function() { + return parseInt(this.rings[0]) * parseInt(this.rings[1]); } +}; + function Frame() { // your code here } function Tire() { - // your code here + this._isFlat = false; +} + +Tire.prototype.isFlat = function() { + return this._isFlat; +} + +Tire.prototype.puncture = function() { + this._isFlat = true; +} + +Tire.prototype.repair = function() { + this._isFlat = false; } module.exports = { From 0522c1cfb012fb3a8eca9b26a0973e9f4d2b8c79 Mon Sep 17 00:00:00 2001 From: James Choi Date: Thu, 11 May 2017 10:08:03 -0700 Subject: [PATCH 4/7] stage3 --- jsinfo/error.js | 7 +++++++ jsinfo/hamsters.js | 15 +++++++++++++++ jsinfo/rewrite_proto.js | 35 +++++++++++++++++++++++++++++++++++ jsinfo/search_algorithm.js | 19 +++++++++++++++++++ npm-debug.log | 4 ++-- 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 jsinfo/error.js create mode 100644 jsinfo/hamsters.js create mode 100644 jsinfo/rewrite_proto.js create mode 100644 jsinfo/search_algorithm.js diff --git a/jsinfo/error.js b/jsinfo/error.js new file mode 100644 index 0000000..5fb2850 --- /dev/null +++ b/jsinfo/error.js @@ -0,0 +1,7 @@ +Rabbit.prototype = Animal.prototype + +Both become the same object so the methods inside each function become jumbled. +In this case, Rabbit.prototype would override Animal.prototype so Animal.prototype inherits the function of Rabbit.prototype. + +Rabbit.prototype._proto = Animal.prototype allows the prototypes to be separate and store methods of the corresponding class, +with Rabbit.prototype inheriting from Animal.prototype. diff --git a/jsinfo/hamsters.js b/jsinfo/hamsters.js new file mode 100644 index 0000000..0d54385 --- /dev/null +++ b/jsinfo/hamsters.js @@ -0,0 +1,15 @@ +let hamster = { + stomach: [], + + eat(food) { + this.stomach = [food]; + } +}; + +let speedy = { + _proto_: hamster +}; + +let lazy = { + _proto_: hamster +}; diff --git a/jsinfo/rewrite_proto.js b/jsinfo/rewrite_proto.js new file mode 100644 index 0000000..f9fafad --- /dev/null +++ b/jsinfo/rewrite_proto.js @@ -0,0 +1,35 @@ +function Clock({template}) { + this._template = template; +} + +Clock.prototype._render = function() { + + let date = new Date(); + + let hours = date.getHours(); + if (hours < 10) hours = '0' + mins; + + let mins = date.getMinutes(); + if (mins < 10) min = '0' + mins; + + let secs = date.getSeconds(); + if (secs < 10) secs = '0' + secs; + + let output = template + .replace('h', hours) + .replace('m', mins) + .replace('s', secs); + + console.log(output); + } + + Clock.prototype.stop = function() { + clearInterval(this._timer); + }; + + Clock.prototype.start = function() { + this._render(); + this._timer = setInterval(() => this._render(), 1000); + }; + +} diff --git a/jsinfo/search_algorithm.js b/jsinfo/search_algorithm.js new file mode 100644 index 0000000..a82b6ec --- /dev/null +++ b/jsinfo/search_algorithm.js @@ -0,0 +1,19 @@ +let head = { + glasses: 1, +}; + +let table = { + pen: 3, + _proto_: head +}; + +let bed = { + sheet: 1, + pillow: 2, + _proto_: table +}; + +let pockets = { + money: 2000 + _proto_: bed +} diff --git a/npm-debug.log b/npm-debug.log index d31a984..4ada9df 100644 --- a/npm-debug.log +++ b/npm-debug.log @@ -2,14 +2,14 @@ 1 verbose cli [ '/usr/local/bin/node', 1 verbose cli '/usr/local/bin/npm', 1 verbose cli 'run', -1 verbose cli 'test:stage2' ] +1 verbose cli 'test:stage3' ] 2 info using npm@3.10.10 3 info using node@v6.10.3 4 verbose stack Error: ENOENT: no such file or directory, open '/Users/jameschoi/LG/package.json' 4 verbose stack at Error (native) 5 verbose cwd /Users/jameschoi/LG/oop-practice 6 error Darwin 16.5.0 -7 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "test:stage2" +7 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "test:stage3" 8 error node v6.10.3 9 error npm v3.10.10 10 error path /Users/jameschoi/LG/package.json From 608c76143b3f0c6d80ae56077801a541ca7832d4 Mon Sep 17 00:00:00 2001 From: James Choi Date: Thu, 11 May 2017 15:40:47 -0700 Subject: [PATCH 5/7] stage4 --- jsinfo/Error_instance.js | 13 +++++++++++ jsinfo/extended_clock.js | 47 ++++++++++++++++++++++++++++++++++++++++ jsinfo/rewrite.js | 32 +++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 jsinfo/Error_instance.js create mode 100644 jsinfo/extended_clock.js create mode 100644 jsinfo/rewrite.js diff --git a/jsinfo/Error_instance.js b/jsinfo/Error_instance.js new file mode 100644 index 0000000..24d5076 --- /dev/null +++ b/jsinfo/Error_instance.js @@ -0,0 +1,13 @@ +class Animal { + + constructor(name) { + this.name = name; + } +} + +class Rabbit extends Animal { + constructor(name) { + super(name); + this.created = Date.now(); + } +} diff --git a/jsinfo/extended_clock.js b/jsinfo/extended_clock.js new file mode 100644 index 0000000..9c93075 --- /dev/null +++ b/jsinfo/extended_clock.js @@ -0,0 +1,47 @@ +class Clock { + constructor({ template }) { + this._template = template; + } + + _render() { + let date = new Date(); + + let hours = date.getHours(); + if (hours < 10) hours = '0' + hours; + + let mins = date.getMinutes(); + if (mins < 10) min = '0' + mins; + + let secs = date.getSeconds(); + if (secs < 10) secs = '0' + secs; + + let output = this._template + .replace('h', hours) + .replace('m', mins) + .replace('s', secs); + + console.log(output); + } + + stop() { + clearInterval(this._timer); + } + + start() { + this._render(); + this._timer = setInterval(() => this._render(), 1000); + } +} + +class ExtendedClock extends Clock { + constructor(options) { + super(options); + let {precision = 1000} = options; + this._precision = precision; + } + + start() { + this._render(); + this._timer = setInterval(() => this._render(), this._precision); + } +}; diff --git a/jsinfo/rewrite.js b/jsinfo/rewrite.js new file mode 100644 index 0000000..6ecf2bd --- /dev/null +++ b/jsinfo/rewrite.js @@ -0,0 +1,32 @@ +class Clock{ + + constructor({template}) { + this._template = template; + } + + _render() { + let date = new Date(); + if (hours < 10) hours = '0' + hours; + + let mins = date.getMinutes(); + if (mins < 10) min = '0' + mins; + + let secs = date.getSeconds(); + if (secs < 10) secs = '0' + secs; + + let output = this._template + .replace('h', hours) + .replace('m', mins) + .replace('s', secs); + + console.log(output); + } + + stop() { + clearInterval(this._timer); + } + + start() { + this._render(); + this._timer = setInterval(() => this._render(), 1000); + }; From 01e396569414346d45a464687c52c8a82c61ec11 Mon Sep 17 00:00:00 2001 From: James Choi Date: Thu, 11 May 2017 16:59:49 -0700 Subject: [PATCH 6/7] stage4done --- bike-shop/src/stage4-inheritance.js | 57 +++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/bike-shop/src/stage4-inheritance.js b/bike-shop/src/stage4-inheritance.js index 2281fd5..a8dae1c 100644 --- a/bike-shop/src/stage4-inheritance.js +++ b/bike-shop/src/stage4-inheritance.js @@ -1,16 +1,67 @@ class Frame { -// your code here + constructor(style){ + this.style = style; + } } class Tire { - // your code here + constructor(diameter, type) { + this.diameter = diameter; + this.type = type; + } } class Bike { - // your code here + constructor() { + this.tires = [new Tire(), new Tire()]; + this.frame = new Frame(); + } +} + +class MountainBike extends Bike { + constructor() { + super(); + this.frame.style = 'mountain'; + this.tires[0].type = 'dirt'; + this.tires[1].type = 'dirt'; + this.shocks = 20; +} + adjustShocks(x) { + this.shocks = x; + } +} + +class BMXBike extends Bike { + constructor() { + super(); + this.brakes = { + front : false, + back : true, + }; + this.tires.diameter[0] = 20; + this.tires.diameter[1] = 20; + } +} + +class RacingBike extends Bike { + constructor() { + super(); + this.frame.style = 'racing'; + this.tires[0].type = 'road'; + this.tires[1].type = 'road'; + this.rings = [3, 10]; + } + gearSpeeds() { + return (parseInt(this.rings[0])) * (parseInt(this.rings[1])); + } } module.exports = { Bike: Bike, // you'll need to export new classes here + Frame: Frame, + Tire: Tire, + MountainBike: MountainBike, + BMXBike: BMXBike, + RacingBike: RacingBike, } From 4a2003a21e9274140feb1117de137461e1aa2f06 Mon Sep 17 00:00:00 2001 From: James Choi Date: Fri, 12 May 2017 12:19:14 -0700 Subject: [PATCH 7/7] FINAL --- bike-shop/.DS_Store | Bin 0 -> 6148 bytes bike-shop/src/stage4-inheritance.js | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 bike-shop/.DS_Store diff --git a/bike-shop/.DS_Store b/bike-shop/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f5a15976aa66450baab8fe8ddd3a94abf5fd0b0d GIT binary patch literal 6148 zcmeHK%}(1u5Z+A!*h(afganm1tkgp$ix^pUWIN;#MKUkY z2Pj;jeGYgD9)t(z%XekU2a)3(S>KBi4|jifC&r6w0Fm8oWN@cm)v!YcN0bbd6_FJ$eU0bI%hqYFKo`9lYT%qt^3K+5!LoAl! cRZuD57ia+58ViNs0ilb4q=6b@;7u9$3vBpb8UO$Q literal 0 HcmV?d00001 diff --git a/bike-shop/src/stage4-inheritance.js b/bike-shop/src/stage4-inheritance.js index a8dae1c..8838582 100644 --- a/bike-shop/src/stage4-inheritance.js +++ b/bike-shop/src/stage4-inheritance.js @@ -36,8 +36,8 @@ class BMXBike extends Bike { super(); this.brakes = { front : false, - back : true, - }; + back : true + } this.tires.diameter[0] = 20; this.tires.diameter[1] = 20; }