diff --git a/bike-shop/.DS_Store b/bike-shop/.DS_Store new file mode 100644 index 0000000..f5a1597 Binary files /dev/null and b/bike-shop/.DS_Store differ 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/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/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 = { diff --git a/bike-shop/src/stage4-inheritance.js b/bike-shop/src/stage4-inheritance.js index 2281fd5..8838582 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, } 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/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; + + + } + +}; 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/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/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/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/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/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); + }; 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 new file mode 100644 index 0000000..4ada9df --- /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: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:stage3" +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 ]