From ce4607b20a3d4ee165032a3cc6e1805f4c205f01 Mon Sep 17 00:00:00 2001 From: Luke Nemy Date: Mon, 1 May 2017 17:58:15 -0700 Subject: [PATCH 1/5] Day 1 specs and exercises --- bike-shop/src/stage1-literals.js | 21 ++++++++++++++++++++- jsinfo/Create_calculator | 20 ++++++++++++++++++++ jsinfo/Hello_Object | 8 ++++++++ jsinfo/Ladder | 17 +++++++++++++++++ jsinfo/Multipy_numeric_properties | 26 ++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 jsinfo/Create_calculator create mode 100644 jsinfo/Hello_Object create mode 100644 jsinfo/Ladder create mode 100644 jsinfo/Multipy_numeric_properties diff --git a/bike-shop/src/stage1-literals.js b/bike-shop/src/stage1-literals.js index c749dd2..950d76b 100644 --- a/bike-shop/src/stage1-literals.js +++ b/bike-shop/src/stage1-literals.js @@ -1,5 +1,24 @@ const myBike = { - // your code here + name: "Roadster", + price: 199.99, + frame: new frame() { + height: 55, + color: 'blue', + stlye: 'cruiser' + } + brakes: new brakes() { + front: false, + back: true + } + tires: new tires() { + diameter: 22, + type: 'fat' + } + rings: new rings() { + front: 2, + back: 5 + } } + module.exports = myBike diff --git a/jsinfo/Create_calculator b/jsinfo/Create_calculator new file mode 100644 index 0000000..ae1ae47 --- /dev/null +++ b/jsinfo/Create_calculator @@ -0,0 +1,20 @@ +//Create Calculator + +let calculator = { + + read() { + this.num1 = +prompt('num1?'); + this.num2 = +prompt('num2?'); + }, + sum() { + return this.num1 + this.num2; + }, + + mul() { + return this.num1 * this.num2; + }, +}; + +calculator.read(); +alert( calculator.sum() ); +alert( calculator.mul() ); diff --git a/jsinfo/Hello_Object b/jsinfo/Hello_Object new file mode 100644 index 0000000..4e53226 --- /dev/null +++ b/jsinfo/Hello_Object @@ -0,0 +1,8 @@ +//Hello Object + +let user = {} +user.name: 'John'; +user.surname: 'Smith'; +user.name = 'Pete'; + +delete user.name diff --git a/jsinfo/Ladder b/jsinfo/Ladder new file mode 100644 index 0000000..a7be386 --- /dev/null +++ b/jsinfo/Ladder @@ -0,0 +1,17 @@ +//Ladder + +let ladder = { + step: 0, + up() { + this.step++; + return this; + }, + down() { + this.step--; + return this; + }, + showStep: function() { // shows the current step + alert( this.step ); + return this; + } +}; diff --git a/jsinfo/Multipy_numeric_properties b/jsinfo/Multipy_numeric_properties new file mode 100644 index 0000000..380196f --- /dev/null +++ b/jsinfo/Multipy_numeric_properties @@ -0,0 +1,26 @@ +//Multipy Numeric Properties by 2 + +// before the call +let menu = { + width: 200, + height: 300, + title: "My menu" +}; + +multiplyNumeric(menu); + + function multiplyNumeric() { + for ( let key in obj ) { + if (typeof obj[key] == 'number') { + obj[key] *= 2; + } + } + } + + +// after the call +menu = { + width: 400, + height: 600, + title: "My menu" +}; From c8cce5eabd1ad1fe24e85df04ffdf53247c29c23 Mon Sep 17 00:00:00 2001 From: Luke Nemy Date: Tue, 2 May 2017 14:47:59 -0700 Subject: [PATCH 2/5] Updated filetypes --- jsinfo/{Create_calculator => Create_calculator.js} | 0 jsinfo/{Hello_Object => Hello_Object.js} | 0 jsinfo/{Ladder => Ladder.js} | 0 .../{Multipy_numeric_properties => Multipy_numeric_properties.js} | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename jsinfo/{Create_calculator => Create_calculator.js} (100%) rename jsinfo/{Hello_Object => Hello_Object.js} (100%) rename jsinfo/{Ladder => Ladder.js} (100%) rename jsinfo/{Multipy_numeric_properties => Multipy_numeric_properties.js} (100%) diff --git a/jsinfo/Create_calculator b/jsinfo/Create_calculator.js similarity index 100% rename from jsinfo/Create_calculator rename to jsinfo/Create_calculator.js diff --git a/jsinfo/Hello_Object b/jsinfo/Hello_Object.js similarity index 100% rename from jsinfo/Hello_Object rename to jsinfo/Hello_Object.js diff --git a/jsinfo/Ladder b/jsinfo/Ladder.js similarity index 100% rename from jsinfo/Ladder rename to jsinfo/Ladder.js diff --git a/jsinfo/Multipy_numeric_properties b/jsinfo/Multipy_numeric_properties.js similarity index 100% rename from jsinfo/Multipy_numeric_properties rename to jsinfo/Multipy_numeric_properties.js From 29ed24d44d970aa68c849e4681513495d858ba7b Mon Sep 17 00:00:00 2001 From: Luke Nemy Date: Tue, 2 May 2017 18:34:52 -0700 Subject: [PATCH 3/5] Day 2 exercises and progression on stage2 of Bike Shop --- bike-shop/src/stage2-constructors.js | 27 +++++++++++++++++++++------ jsinfo/Extendable_Calculator.js | 27 +++++++++++++++++++++++++++ jsinfo/New_Accumulator.js | 16 ++++++++++++++++ jsinfo/New_Calculator.js | 23 +++++++++++++++++++++++ 4 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 jsinfo/Extendable_Calculator.js create mode 100644 jsinfo/New_Accumulator.js create mode 100644 jsinfo/New_Calculator.js diff --git a/bike-shop/src/stage2-constructors.js b/bike-shop/src/stage2-constructors.js index c422386..9ca47b6 100644 --- a/bike-shop/src/stage2-constructors.js +++ b/bike-shop/src/stage2-constructors.js @@ -1,15 +1,30 @@ -function Bike() { - // your code here -} -function Frame() { - // your code here +function Frame(color, size, style) { + this.color = 'black'; + this.size = 55; + this.style = 'street'; } +let myFrame = new Frame(); + +function myFrame() { + this.color = 'blue', + this.size = 60, + this.style = 'mountain' +}; + +// myFrame.color = 'blue'; +// myFrame.size = 60; +// myFrame.style = 'mountain'; + function Tire() { - // your code here } +function Bike() { + +} + + module.exports = { Bike: Bike, Frame: Frame, diff --git a/jsinfo/Extendable_Calculator.js b/jsinfo/Extendable_Calculator.js new file mode 100644 index 0000000..3d3114f --- /dev/null +++ b/jsinfo/Extendable_Calculator.js @@ -0,0 +1,27 @@ +// Extendable Calculator + +function Calculator() { + + let methods = { + "-": (a, b) => a - b, + "+": (a, b) => a + b + }; + + this.calculate = function(str) { + + let split = str.split(' '), + a = +split[0], + op = split[1], + b = +split[2] + + if (!methods[op] || isNaN(a) || isNaN(b)) { + return NaN; + } + + return methods[op](a, b); + } + + this.addMethod = function(name, func) { + methods[name] = func; + }; +} diff --git a/jsinfo/New_Accumulator.js b/jsinfo/New_Accumulator.js new file mode 100644 index 0000000..da7a4c4 --- /dev/null +++ b/jsinfo/New_Accumulator.js @@ -0,0 +1,16 @@ +//New Accumulator + +function Accumulator(startingValue) { + this.value = startingValue; + + this.read = function() { + this.value += +prompt('How much to add?', 0); + }; +} + + +let accumulator = new Accumulator(1); +accumulator.read(); +accumulator.read(); + +alert(accumulator.value) diff --git a/jsinfo/New_Calculator.js b/jsinfo/New_Calculator.js new file mode 100644 index 0000000..612ecb7 --- /dev/null +++ b/jsinfo/New_Calculator.js @@ -0,0 +1,23 @@ +//New Calculator + +function Calculator() { + + this.read = function() { + this.a = +prompt('a?', 0); + this.b = +prompt('b?', 0); + }; + + this.sum = function() { + return this.a + this.b; + }; + + this.mul = function() { + return this.a * this.b; + }; +} + +let calculator = new Calculator(); +calculator.read(); + +alert( "Sum=" + calculator.sum() ); +alert( "Mul=" + calculator.mul() ); From 02a6edc330cfaa83f4ba1932a22f8b3eee22519b Mon Sep 17 00:00:00 2001 From: Luke Nemy Date: Thu, 4 May 2017 13:54:36 -0700 Subject: [PATCH 4/5] edited stage2 file --- bike-shop/src/stage2-constructors.js | 37 +++++++++++++--------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/bike-shop/src/stage2-constructors.js b/bike-shop/src/stage2-constructors.js index 9ca47b6..2004206 100644 --- a/bike-shop/src/stage2-constructors.js +++ b/bike-shop/src/stage2-constructors.js @@ -1,30 +1,27 @@ -function Frame(color, size, style) { - this.color = 'black'; - this.size = 55; - this.style = 'street'; +function Frame(color = 'black', size = 55, style = 'street') { + this.color = color, + this.size = size, + this.style = style } -let myFrame = new Frame(); - -function myFrame() { - this.color = 'blue', - this.size = 60, - this.style = 'mountain' -}; - -// myFrame.color = 'blue'; -// myFrame.size = 60; -// myFrame.style = 'mountain'; - -function Tire() { +function Tire(diameter = 22, type = 'street') { + this.diameter = diameter, + this.type = type } -function Bike() { - +function Bike(name = 'Speedster', price = 250.00) { + this.name = name, + this.price = price, + this.tires = [new Tire(), new Tire()], + this.frame = new Frame(), + this.rings = [3, 7], + this.brakes = { + front: true, + back: true + } } - module.exports = { Bike: Bike, Frame: Frame, From e799b04286b8f9d5aa91b9105610c03d31cb35e5 Mon Sep 17 00:00:00 2001 From: Luke Nemy Date: Thu, 4 May 2017 14:03:29 -0700 Subject: [PATCH 5/5] stage3 commit --- bike-shop/src/stage3-methods.js | 48 +++++++++++++++++++++++++++++---- jsinfo/Inheritance_error.js | 22 +++++++++++++++ jsinfo/Prototype_Rewrite.js | 36 +++++++++++++++++++++++++ jsinfo/Searching_Algorithm.js | 21 +++++++++++++++ jsinfo/Why_two_hamsters_full.js | 24 +++++++++++++++++ 5 files changed, 146 insertions(+), 5 deletions(-) create mode 100644 jsinfo/Inheritance_error.js create mode 100644 jsinfo/Prototype_Rewrite.js create mode 100644 jsinfo/Searching_Algorithm.js create mode 100644 jsinfo/Why_two_hamsters_full.js diff --git a/bike-shop/src/stage3-methods.js b/bike-shop/src/stage3-methods.js index c422386..0e666c7 100644 --- a/bike-shop/src/stage3-methods.js +++ b/bike-shop/src/stage3-methods.js @@ -1,17 +1,55 @@ function Bike() { // your code here + this.moving = false, + this.tires = [new Tire(), new Tire()], + this.rings = [3, 7] + } -function Frame() { - // your code here +Bike.prototype.isMoving = function() { + return this.moving } -function Tire() { - // your code here +Bike.prototype.pedal = function() { + if (this.tires[0]._flat === true || this.tires[1]._flat === true ) { + throw new Error("Can't pedal with a flat tire") + } else { + + this.moving = true + } +} + +Bike.prototype.brake = function() { + return this.moving = false } +Bike.prototype.gearSpeeds = function() { + return this.rings.reduce((acc, next) => { + return acc * next + }, 1) +}; + +function Tire() { + this._flat = false + } + + Tire.prototype.isFlat = function() { + return this._flat + } + + Tire.prototype.puncture = function() { + this._flat = true + } + + Tire.prototype.repair = function() { + this._flat = false + } + + + + module.exports = { Bike: Bike, - Frame: Frame, + //Frame: Frame, Tire: Tire } diff --git a/jsinfo/Inheritance_error.js b/jsinfo/Inheritance_error.js new file mode 100644 index 0000000..9d6cb2d --- /dev/null +++ b/jsinfo/Inheritance_error.js @@ -0,0 +1,22 @@ +//An error in inheritance + +function Animal(name) { + this.name = name; +} + +Animal.prototype.walk = function() { + alert(this.name + ' walks'); +}; + +function Rabbit(name) { + this.name = name; +} + +//--> Rabbit.prototype = Animal.prototype <-- + //error here + +Rabbit.protoype.__proto__ = Animal.prototype; //<-- fix + +Rabbit.prototype.walk = function() { + alert(this.name + " bounces!"); +}; diff --git a/jsinfo/Prototype_Rewrite.js b/jsinfo/Prototype_Rewrite.js new file mode 100644 index 0000000..1cd68b3 --- /dev/null +++ b/jsinfo/Prototype_Rewrite.js @@ -0,0 +1,36 @@ +//Rewrite to Prototype + +function Clock({ template }) { + this._template = template; +} + + Clock.prototype._render = function() { + 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); + } + + Clock.prototype.stop = function() { + clearInterval(this._timer); + }; + + Clock.prototype.start = function() { + this._render(); + this._timer = setInterval(() => ._render(), 1000); + }; + +} diff --git a/jsinfo/Searching_Algorithm.js b/jsinfo/Searching_Algorithm.js new file mode 100644 index 0000000..7cabe92 --- /dev/null +++ b/jsinfo/Searching_Algorithm.js @@ -0,0 +1,21 @@ +//Searching algorithm + +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/jsinfo/Why_two_hamsters_full.js b/jsinfo/Why_two_hamsters_full.js new file mode 100644 index 0000000..e29f9c4 --- /dev/null +++ b/jsinfo/Why_two_hamsters_full.js @@ -0,0 +1,24 @@ +//Why Are two hamsters full? + +let hamster = { + stomach: [], + + eat(food) { + this.stomach = [food]; //<-- fix + } +}; + +let speedy = { + __proto__: hamster +}; + +let lazy = { + __proto__: hamster +}; + +// This one found the food +speedy.eat("apple"); +alert( speedy.stomach ); // apple + +// This one also has it, why? fix please. +alert( lazy.stomach ); // apple