From 6a0385c2d4d6c844643f445ecb026e2d4a339531 Mon Sep 17 00:00:00 2001 From: Alex Reany Date: Mon, 24 Apr 2017 12:57:32 -0700 Subject: [PATCH 01/11] stage 1 complete --- index.html | 10 ++++++++ jsinfo/chaining.js | 46 ++++++++++++++++++++++++++++++++++ jsinfo/createACalc.js | 20 +++++++++++++++ jsinfo/helloObject.js | 15 +++++++++++ jsinfo/multiplyNumericByTwo.js | 23 +++++++++++++++++ 5 files changed, 114 insertions(+) create mode 100644 index.html create mode 100644 jsinfo/chaining.js create mode 100644 jsinfo/createACalc.js create mode 100644 jsinfo/helloObject.js create mode 100644 jsinfo/multiplyNumericByTwo.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..449f707 --- /dev/null +++ b/index.html @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/jsinfo/chaining.js b/jsinfo/chaining.js new file mode 100644 index 0000000..e7247ad --- /dev/null +++ b/jsinfo/chaining.js @@ -0,0 +1,46 @@ +// There’s a ladder object that allows to go up and down +/* +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; + + } +}; + +we need to make several calls in sequence, can do it like this: +ladder.up(); +ladder.up(); +ladder.down(); +ladder.showStep(); // 1 +*/ + +//rewrite the code to make it chainable +let ladder = { + step: 0, + up() { + this.step++; + return this; + }, + down() { + this.step--; + return this; + + }, + // shows the current step + showStep: function() { + alert( this.step ); + return this; + } +}; +ladder.up().up().down().showStep(); // 1 diff --git a/jsinfo/createACalc.js b/jsinfo/createACalc.js new file mode 100644 index 0000000..ac12bf2 --- /dev/null +++ b/jsinfo/createACalc.js @@ -0,0 +1,20 @@ +// Create an object calculator with three methods: read , sum, mul + +let calculator = { + read() { + this.value1 = parseFloat(prompt("what is the first value")); + this.value2 = parseFloat(prompt("what is the second value")); + }, + + sum() { + return this.value1 + this.value2; + }, + + mul() { + return this.value1 * this.value2; + } +} + +calculator.read(); +alert( calculator.sum() ); +alert( calculator.mul() ); diff --git a/jsinfo/helloObject.js b/jsinfo/helloObject.js new file mode 100644 index 0000000..4c037f7 --- /dev/null +++ b/jsinfo/helloObject.js @@ -0,0 +1,15 @@ +//Hello, object + +//Creating an object + + + +let user = { + name : "John", + surname : "Smith" +}; +// reassign name to "Pete" + +user.name = "Pete"; +// Delete the property of name +delete user.name; diff --git a/jsinfo/multiplyNumericByTwo.js b/jsinfo/multiplyNumericByTwo.js new file mode 100644 index 0000000..fd901ce --- /dev/null +++ b/jsinfo/multiplyNumericByTwo.js @@ -0,0 +1,23 @@ +/* Create a function multiplyNumeric(obj) that multiplies + all numeric properties of obj by 2.*/ + + function multiplyNumeric(obj) { + //gets values of the keys in the object + for (let key in obj) { + /*makes sure that the type of is number + so you can actually multiply by two*/ + if (typeof obj[key] === 'number') { + obj[key] *= 2; + } + } + } + +//example object + let menu = { + width: 200, + height: 300, + title: "My menu" + }; + +//call the function + multiplyNumeric(menu) From 0cb9ebe706582930cc13295ec750350152461021 Mon Sep 17 00:00:00 2001 From: Alex Reany Date: Mon, 24 Apr 2017 17:45:50 -0700 Subject: [PATCH 02/11] finished stage 1 --- bike-shop/src/stage1-literals.js | 20 ++++++++++++++++++-- index.html | 2 +- jsinfo/chaining.js | 1 - jsinfo/createNewAccumlator.js | 28 ++++++++++++++++++++++++++++ jsinfo/createNewCalc.js | 29 +++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 jsinfo/createNewAccumlator.js create mode 100644 jsinfo/createNewCalc.js diff --git a/bike-shop/src/stage1-literals.js b/bike-shop/src/stage1-literals.js index c749dd2..e18a7e2 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 : { + color : "blue", + height: 55, + style : "cruiser" + }, + brakes : { + back : true, + front : false + }, + tires : { + diameter : 22, + type : "fat" + }, + rings : [2, 5] +}; module.exports = myBike diff --git a/index.html b/index.html index 449f707..086ca1a 100644 --- a/index.html +++ b/index.html @@ -5,6 +5,6 @@ - + diff --git a/jsinfo/chaining.js b/jsinfo/chaining.js index e7247ad..4a7e52d 100644 --- a/jsinfo/chaining.js +++ b/jsinfo/chaining.js @@ -35,7 +35,6 @@ let ladder = { down() { this.step--; return this; - }, // shows the current step showStep: function() { diff --git a/jsinfo/createNewAccumlator.js b/jsinfo/createNewAccumlator.js new file mode 100644 index 0000000..c7f06f8 --- /dev/null +++ b/jsinfo/createNewAccumlator.js @@ -0,0 +1,28 @@ +/*Create a constructor function Accumulator(startingValue). + +Object that it creates should: + +Store the “current value” in the property value. The starting value is set to the argument of the constructor startingValue. +The read() method should use prompt to read a new number and add it to value. +In other words, the value property is the sum of all user-entered values with the initial value startingValue. +*/ + + +function Accumulator(startingValue) { + + this.read = function () { + this.value1 = parseFloat(prompt("what is the first value")); + startingValue += this.value1; + console.log(startingValue); + }; + + this.value = function (startingValue) { + return Accumulator.startingValue + } +} + + +let accumulator = new Accumulator(1); // initial value 1 +accumulator.read(); // adds the user-entered value +accumulator.read(); // adds the user-entered value +alert(accumulator.value); // shows the sum of these values diff --git a/jsinfo/createNewCalc.js b/jsinfo/createNewCalc.js new file mode 100644 index 0000000..836aa8c --- /dev/null +++ b/jsinfo/createNewCalc.js @@ -0,0 +1,29 @@ +/*Create a constructor function Calculator +that creates objects with 3 methods: +read() sum() mul() */ + +function Calculator() { + + this.read = function () { + this.value1 = parseFloat(prompt("what is the first value")); + this.value2 = parseFloat(prompt("what is the second value")); + }; + + this.sum = function () { + this.sumResults = this.value1 + this.value2; + return this.sumResults; + }; + + this.mul = function () { + this.mulResults = this.value1 * this.value2; + return this.mulResults; + }; + +} + +let calculator = new Calculator(); + +calculator.read(); + +alert( "Sum=" + calculator.sum() ); +alert( "Mul=" + calculator.mul() ); From f9eaf5363eb65062fcf2cbc303b15df1df80aaaa Mon Sep 17 00:00:00 2001 From: Alex Reany Date: Tue, 25 Apr 2017 11:39:13 -0700 Subject: [PATCH 03/11] finished stage 2 --- bike-shop/src/stage2-constructors.js | 43 ++++++++++++++++++++++++---- index.html | 2 +- jsinfo/createNewAccumlator.js | 11 +++---- jsinfo/extendableCalculator.js | 40 ++++++++++++++++++++++++++ jsinfo/searthingAlgo.js | 0 5 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 jsinfo/extendableCalculator.js create mode 100644 jsinfo/searthingAlgo.js diff --git a/bike-shop/src/stage2-constructors.js b/bike-shop/src/stage2-constructors.js index c422386..b52ede6 100644 --- a/bike-shop/src/stage2-constructors.js +++ b/bike-shop/src/stage2-constructors.js @@ -1,17 +1,48 @@ -function Bike() { - // your code here +function Bike(name, price) { + alert(new.target); + this.name = name + this.price = price + this.rings = [3, 7] + this.brakes = { + back : true, + front : true + }, + this.tires = [new Tire, new Tire] + this.frame = new Frame } -function Frame() { - // your code here +function Frame(color, size, style) { + this.color = color ? color : "black" + this.size = size ? size : 55 + this.style = style ? style : "street" } -function Tire() { - // your code here +function Tire(diameter, type) { + this.diameter = diameter ? diameter : 22 + this.type = type ? type : 'street' + this.rings = [2, 5] } + module.exports = { Bike: Bike, Frame: Frame, Tire: Tire } + + + + +const myFrame = new Frame() +//myFrame.color = "black" +//myFrame.size = 55 +//myFrame.style = "street" + +const myBike = new Bike() +//myBike.name = "" +//myBike.price = "" +//myBike.tires = ["front", "back"] + +const myTire = new Tire() +//myTire.diameter = "" +//myTire.type = "" diff --git a/index.html b/index.html index 086ca1a..d873de8 100644 --- a/index.html +++ b/index.html @@ -5,6 +5,6 @@ - + diff --git a/jsinfo/createNewAccumlator.js b/jsinfo/createNewAccumlator.js index c7f06f8..dceab73 100644 --- a/jsinfo/createNewAccumlator.js +++ b/jsinfo/createNewAccumlator.js @@ -9,16 +9,13 @@ In other words, the value property is the sum of all user-entered values with th function Accumulator(startingValue) { + //holds the startingValue to be minipulated + this.value = startingValue; this.read = function () { - this.value1 = parseFloat(prompt("what is the first value")); - startingValue += this.value1; - console.log(startingValue); + //adds the prompted amount to the value at the moment + this.value += parseFloat(prompt("what is the first value")); }; - - this.value = function (startingValue) { - return Accumulator.startingValue - } } diff --git a/jsinfo/extendableCalculator.js b/jsinfo/extendableCalculator.js new file mode 100644 index 0000000..3c975a7 --- /dev/null +++ b/jsinfo/extendableCalculator.js @@ -0,0 +1,40 @@ +//Create a calculator that can take a string and return the value +function Calculator() { + + let methods = { + "+" : function(value1, value2){ + return parseFloat(value1) + parseFloat(value2) + }, + //arrow function + "-" : (value1, value2) => parseFloat(value1) - parseFloat(value2), + } + + this.calculate = function (str) { + + let strSplit = str.split(" "); + var value1 = strSplit[0]; + var value2 = strSplit[2]; + var operator = strSplit[1]; + + return methods[operator](value1, value2) + }; + + this.addOperator =function (name, func){ + methods[name] = func; + } +} + +let calc = new Calculator; + +alert( calc.calculate("3 + 7") ); // 10 + +let newCalc = new Calculator + +newCalc.addOperator("*", (a, b) => a * b); +newCalc.addOperator("/", (a, b) => a / b); +newCalc.addOperator("**", (a, b) => a ** b) +newCalc.addOperator("pie", (a,b) => "I have " + a + " pies cut into " + b + " slices each") +alert(newCalc.calculate("2 * 3")); //6 +alert(newCalc.calculate("2 ** 3")); //8 +alert(newCalc.calculate("6 / 2")); //3 +alert(newCalc.calculate("7 pie 4")); diff --git a/jsinfo/searthingAlgo.js b/jsinfo/searthingAlgo.js new file mode 100644 index 0000000..e69de29 From 023bd6c18bc50ddc49301363cf61800644d0b80f Mon Sep 17 00:00:00 2001 From: Alex Reany Date: Tue, 25 Apr 2017 11:43:41 -0700 Subject: [PATCH 04/11] cleaning up branches --- jsinfo/searthingAlgo.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/jsinfo/searthingAlgo.js b/jsinfo/searthingAlgo.js index e69de29..05d877f 100644 --- a/jsinfo/searthingAlgo.js +++ b/jsinfo/searthingAlgo.js @@ -0,0 +1,22 @@ +/* +Use __proto__ to assign prototypes in a way that any property +lookup will follow the path: pockets → bed → table → head. For instance, +pockets.pen should be 3 (found in table), and bed.glasses should be 1 (found in head). +*/ + +let head = { + glasses: 1 +}; + +let table = { + pen: 3 +}; + +let bed = { + sheet: 1, + pillow: 2 +}; + +let pockets = { + money: 2000 +}; From d1cf8821d3569183ae4de71b76e466c384751f38 Mon Sep 17 00:00:00 2001 From: Alex Reany Date: Wed, 26 Apr 2017 08:17:52 -0700 Subject: [PATCH 05/11] work in progress --- bike-shop/src/stage3-methods.js | 41 ++++++++++++++++- index.html | 2 +- jsinfo/createACalc.js | 1 + jsinfo/createNewCalc.js | 1 + jsinfo/errorInInheritance.js | 27 +++++++++++ jsinfo/extendableCalculator.js | 2 + jsinfo/rewritePrototype.js | 79 ++++++++++++++++++++++++++++++++ jsinfo/rewriteToClass.js | 81 +++++++++++++++++++++++++++++++++ jsinfo/searthingAlgo.js | 15 ++++-- jsinfo/twoHamsters.js | 30 ++++++++++++ 10 files changed, 273 insertions(+), 6 deletions(-) create mode 100644 jsinfo/errorInInheritance.js create mode 100644 jsinfo/rewritePrototype.js create mode 100644 jsinfo/rewriteToClass.js create mode 100644 jsinfo/twoHamsters.js diff --git a/bike-shop/src/stage3-methods.js b/bike-shop/src/stage3-methods.js index c422386..cd42d4e 100644 --- a/bike-shop/src/stage3-methods.js +++ b/bike-shop/src/stage3-methods.js @@ -6,8 +6,8 @@ function Frame() { // your code here } -function Tire() { - // your code here +function Tire(isFlat) { + isFlat : isFlat } module.exports = { @@ -15,3 +15,40 @@ module.exports = { Frame: Frame, Tire: Tire } + + +Tire.prototype.isFlat = function () { + this.isFlat = false; + return this.isFlat +} + +Tire.prototype.puncture = function () { + this.isFlat = true; + return this +} + +Tire.prototype.repair = function () { + this.isFlat = false; + return this +} + +Bike.prototype.isMoving = function () { + return false +} + +Bike.prototype.pedal = function () { + +} + +Bike.prototype.brake = function () { + +} + +Bike.prototype.gearSpeeds = function () { + +} + + + +const myTire = new Tire +const myBike = new Bike diff --git a/index.html b/index.html index d873de8..2223032 100644 --- a/index.html +++ b/index.html @@ -5,6 +5,6 @@ - + diff --git a/jsinfo/createACalc.js b/jsinfo/createACalc.js index ac12bf2..d852b06 100644 --- a/jsinfo/createACalc.js +++ b/jsinfo/createACalc.js @@ -2,6 +2,7 @@ let calculator = { read() { + // get values this.value1 = parseFloat(prompt("what is the first value")); this.value2 = parseFloat(prompt("what is the second value")); }, diff --git a/jsinfo/createNewCalc.js b/jsinfo/createNewCalc.js index 836aa8c..a3922fe 100644 --- a/jsinfo/createNewCalc.js +++ b/jsinfo/createNewCalc.js @@ -5,6 +5,7 @@ read() sum() mul() */ function Calculator() { this.read = function () { + // get values this.value1 = parseFloat(prompt("what is the first value")); this.value2 = parseFloat(prompt("what is the second value")); }; diff --git a/jsinfo/errorInInheritance.js b/jsinfo/errorInInheritance.js new file mode 100644 index 0000000..589f8d2 --- /dev/null +++ b/jsinfo/errorInInheritance.js @@ -0,0 +1,27 @@ +/* +Find an error in the prototypal inheritance below. +What’s wrong? What are consequences going to be? +*/ + +function Animal(name) { + this.name = name; +} + +Animal.prototype.walk = function() { + alert(this.name + ' walks'); +}; + +function Rabbit(name) { + this.name = name; +} + +//Error it will overwrite all of Rabbit's prototype to Animal's +Rabbit.prototype = Animal.prototype; + +//This will overwrite the previous walk prototype and animals will now bounce and not walk +Rabbit.prototype.walk = function() { + alert(this.name + " bounces!"); +}; + +let rabbit = new Rabbit; +let animal = new Animal("skunk"); diff --git a/jsinfo/extendableCalculator.js b/jsinfo/extendableCalculator.js index 3c975a7..7695075 100644 --- a/jsinfo/extendableCalculator.js +++ b/jsinfo/extendableCalculator.js @@ -1,6 +1,7 @@ //Create a calculator that can take a string and return the value function Calculator() { +//holds new method funtions let methods = { "+" : function(value1, value2){ return parseFloat(value1) + parseFloat(value2) @@ -16,6 +17,7 @@ function Calculator() { var value2 = strSplit[2]; var operator = strSplit[1]; +//return the value of the string to be operated on return methods[operator](value1, value2) }; diff --git a/jsinfo/rewritePrototype.js b/jsinfo/rewritePrototype.js new file mode 100644 index 0000000..44af2da --- /dev/null +++ b/jsinfo/rewritePrototype.js @@ -0,0 +1,79 @@ +//The Clock class is written in functional style. Rewrite it using prototypes. + +/*rewrite this +function Clock({ template }) { + + let timer; + + function 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 = template + .replace('h', hours) + .replace('m', mins) + .replace('s', secs); + + console.log(output); + } + + this.stop = function() { + clearInterval(timer); + }; + + this.start = function() { + render(); + timer = setInterval(render, 1000); + }; + +} + +let clock = new Clock({template: 'h:m:s'}); + clock.start(); + */ + +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(() =>this.render(), 1000); +}; + + + +let clock = new Clock({template: 'h:m:s'}); + clock.start(); diff --git a/jsinfo/rewriteToClass.js b/jsinfo/rewriteToClass.js new file mode 100644 index 0000000..21edd44 --- /dev/null +++ b/jsinfo/rewriteToClass.js @@ -0,0 +1,81 @@ +//Rewrite the Clock class from prototypes to the modern “class” syntax. + +/*rewrite this +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(() => this._render(), 1000); +}; + +let clock = new Clock({template: 'h:m:s'}); + clock.start(); +*/ + +class Clock { + // ({ ... }) allows the item to be passed in as a object literal + constructor({template}){ + console.log(typeof({template})); + this._template = template; + } +// prototype not allowed in classes. bound to the class function + _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); + } + +//clock.stop() will stop the clock + stop() { + clearInterval(this._timer); + }; + +//clock.start() would then restart the clock + start() { + this._render(); + this._timer = setInterval(() => this._render(), 1000); + } +} + +let clock = new Clock({template: 'h:m:s'}); + clock.start(); diff --git a/jsinfo/searthingAlgo.js b/jsinfo/searthingAlgo.js index 05d877f..9d95d9e 100644 --- a/jsinfo/searthingAlgo.js +++ b/jsinfo/searthingAlgo.js @@ -9,14 +9,23 @@ let head = { }; let table = { - pen: 3 + pen: 3, + __proto__ : head }; let bed = { sheet: 1, - pillow: 2 + pillow: 2, + __proto__ : table }; let pockets = { - money: 2000 + money: 2000, + __proto__ : bed }; + + /*is it faster to get glasses as pocket.glasses + or head.glasses? Benchmark if needed.*/ + +//It seems that pocket.glasses and head.glasses +//take about the same amout of time diff --git a/jsinfo/twoHamsters.js b/jsinfo/twoHamsters.js new file mode 100644 index 0000000..6703176 --- /dev/null +++ b/jsinfo/twoHamsters.js @@ -0,0 +1,30 @@ +/*We have two hamsters: speedy and lazy inheriting from the general hamster object. +When we feed one of them, the other one is also full. Why? How to fix it? +*/ + + let hamster = { + stomach: [], + + eat(food) { + //this adds apple to the hamster constructor function + //this.stomach.push(food) + + //This adds apple to the stomach of the hamster object that called the method + this.stomach = [food]; + } +}; + +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 From b4160354a5a218c3864e6ecd9ec6a1746b830ca0 Mon Sep 17 00:00:00 2001 From: Alex Reany Date: Wed, 26 Apr 2017 10:57:24 -0700 Subject: [PATCH 06/11] finalized stage 3 --- bike-shop/src/stage2-constructors.js | 1 - bike-shop/src/stage3-methods.js | 54 ++++++++++++------- jsinfo/{searthingAlgo.js => searchingAlgo.js} | 2 +- 3 files changed, 35 insertions(+), 22 deletions(-) rename jsinfo/{searthingAlgo.js => searchingAlgo.js} (93%) diff --git a/bike-shop/src/stage2-constructors.js b/bike-shop/src/stage2-constructors.js index b52ede6..4c26afb 100644 --- a/bike-shop/src/stage2-constructors.js +++ b/bike-shop/src/stage2-constructors.js @@ -1,5 +1,4 @@ function Bike(name, price) { - alert(new.target); this.name = name this.price = price this.rings = [3, 7] diff --git a/bike-shop/src/stage3-methods.js b/bike-shop/src/stage3-methods.js index cd42d4e..2f2fc5a 100644 --- a/bike-shop/src/stage3-methods.js +++ b/bike-shop/src/stage3-methods.js @@ -1,54 +1,68 @@ function Bike() { - // your code here + + this.rings = [3, 7] + this.brakes = { + back : true, + front : true + }, + this.tires = [new Tire, new Tire], + this.frame = new Frame, + + this._isMoving = false } function Frame() { // your code here } -function Tire(isFlat) { - isFlat : isFlat +function Tire() { + this._isFlat = false } -module.exports = { - Bike: Bike, - Frame: Frame, - Tire: Tire -} + Tire.prototype.isFlat = function () { - this.isFlat = false; - return this.isFlat + return this._isFlat } Tire.prototype.puncture = function () { - this.isFlat = true; - return this + this._isFlat = true + //this.tires[0]._isFlat = true } + Tire.prototype.repair = function () { - this.isFlat = false; - return this + this._isFlat = false; } Bike.prototype.isMoving = function () { - return false + return this._isMoving } Bike.prototype.pedal = function () { - + if (this.tires[0]._isFlat === true) { + throw "Can't pedal with a flat tire" + }else { + this._isMoving = true + } } Bike.prototype.brake = function () { - + this._isMoving = false } Bike.prototype.gearSpeeds = function () { - + return this.rings[0] * this.rings[1] } -const myTire = new Tire -const myBike = new Bike +const myTire = new Tire() +const myBike = new Bike() + +module.exports = { + Bike: Bike, + Frame: Frame, + Tire: Tire +} diff --git a/jsinfo/searthingAlgo.js b/jsinfo/searchingAlgo.js similarity index 93% rename from jsinfo/searthingAlgo.js rename to jsinfo/searchingAlgo.js index 9d95d9e..7e9c08b 100644 --- a/jsinfo/searthingAlgo.js +++ b/jsinfo/searchingAlgo.js @@ -28,4 +28,4 @@ let pockets = { or head.glasses? Benchmark if needed.*/ //It seems that pocket.glasses and head.glasses -//take about the same amout of time +//take about the same amout of time From e64b4ec208bcf7ec5eea2b4ecd68d0dad1706564 Mon Sep 17 00:00:00 2001 From: Alex Reany Date: Wed, 26 Apr 2017 15:19:37 -0700 Subject: [PATCH 07/11] finished stage 4 --- bike-shop/src/stage3-methods.js | 1 - bike-shop/src/stage4-inheritance.js | 59 ++++++++++++++++++++++++++++- index.html | 2 +- jsinfo/errorCreatingAnInstance.js | 26 +++++++++++++ jsinfo/extendClock.js | 57 ++++++++++++++++++++++++++++ jsinfo/rewritePrototype.js | 4 +- jsinfo/rewriteToClass.js | 1 + 7 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 jsinfo/errorCreatingAnInstance.js create mode 100644 jsinfo/extendClock.js diff --git a/bike-shop/src/stage3-methods.js b/bike-shop/src/stage3-methods.js index 2f2fc5a..e84e96a 100644 --- a/bike-shop/src/stage3-methods.js +++ b/bike-shop/src/stage3-methods.js @@ -28,7 +28,6 @@ Tire.prototype.isFlat = function () { Tire.prototype.puncture = function () { this._isFlat = true - //this.tires[0]._isFlat = true } diff --git a/bike-shop/src/stage4-inheritance.js b/bike-shop/src/stage4-inheritance.js index 2281fd5..e139f94 100644 --- a/bike-shop/src/stage4-inheritance.js +++ b/bike-shop/src/stage4-inheritance.js @@ -3,14 +3,69 @@ class Frame { } class Tire { - // your code here + } class Bike { - // your code here + constructor() { + this.tires = [new Tire, new Tire] + this.frame = new Frame + this.brakes = { + back : true, + front : true + } + this.rings = [3, 7] + } +} + +class MountainBike extends Bike { + constructor() { + super(); + this.tires[0].type = 'dirt'; + this.tires[1].type = 'dirt'; + this.frame.style = 'mountain'; + this.shocks = 20; + } + adjustShocks(newSagSetting){ + this.shocks = newSagSetting; + } +} + +class BMXBike extends Bike { + constructor() { + super(); + this.brakes.front = false; + this.tires[0].diameter = 20; + this.tires[1].diameter = 20; + } +} + +class RacingBike extends Bike { + constructor(val) { + super(val); + this.tires[0].type = 'road'; + this.tires[1].type = 'road'; + this.frame.style = 'racing'; + //work on this below + this.rings[0] = 3; + this.rings[1] = 10; + } + gearSpeeds(){ + return this.rings[0] * this.rings[1]; + } } + + +//let mountainBike = new MountainBike() +//let racingBike = new RacingBike() + + + module.exports = { Bike: Bike, + MountainBike: MountainBike, + BMXBike: BMXBike, + RacingBike: RacingBike // you'll need to export new classes here } diff --git a/index.html b/index.html index 2223032..82f542f 100644 --- a/index.html +++ b/index.html @@ -5,6 +5,6 @@ - + diff --git a/jsinfo/errorCreatingAnInstance.js b/jsinfo/errorCreatingAnInstance.js new file mode 100644 index 0000000..9a2eb28 --- /dev/null +++ b/jsinfo/errorCreatingAnInstance.js @@ -0,0 +1,26 @@ +/* +Here’s the code with Rabbit extending Animal. + +Unfortunately, Rabbit objects can’t be created. +What’s wrong? Fix it. +*/ +class Animal { + + constructor(name) { + this.name = name; + } + +} + +class Rabbit extends Animal { + constructor(name) { + //need to add a supper function to allow inheritance + super(name) + //now with the super() + //this.name = name; + this.created = Date.now(); + } +} + +let rabbit = new Rabbit("White Rabbit"); // Error: this is not defined +alert(rabbit.name); diff --git a/jsinfo/extendClock.js b/jsinfo/extendClock.js new file mode 100644 index 0000000..238148c --- /dev/null +++ b/jsinfo/extendClock.js @@ -0,0 +1,57 @@ +class Clock { + // ({ ... }) allows the item to be passed in as a object literal + constructor({template}){ + this._template = template; + } +// prototype not allowed in classes. bound to the class function + _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); + } + +//clock.stop() will stop the clock + stop() { + clearInterval(this._timer); + console.log("clocked stopped"); + }; + +//clock.start() would then restart the clock + start() { + this._render(); + //setInterval(function, milliseconds, param1, param2, ...) + this._timer = setInterval(() => this._render(), this._precision); + } +} + +// pass inheritence from Clock to ExtendedClock +class ExtendedClock extends Clock { + + constructor(val) { + //allows for the inheritence to be manipulated + super(val) + //set the variable + let {precision=1000} = val; + this._precision = precision; + } +} + +let clock = new ExtendedClock({ + template: 'h:m:s', + precision: 1000 +}); + clock.start(); diff --git a/jsinfo/rewritePrototype.js b/jsinfo/rewritePrototype.js index 44af2da..e83108c 100644 --- a/jsinfo/rewritePrototype.js +++ b/jsinfo/rewritePrototype.js @@ -69,8 +69,8 @@ Clock.prototype.stop = function() { }; Clock.prototype.start = function() { - this.render(); - this.timer = setInterval(() =>this.render(), 1000); + this._render(); + this.timer = setInterval(() =>this._render(), 1000); }; diff --git a/jsinfo/rewriteToClass.js b/jsinfo/rewriteToClass.js index 21edd44..68f1be7 100644 --- a/jsinfo/rewriteToClass.js +++ b/jsinfo/rewriteToClass.js @@ -41,6 +41,7 @@ let clock = new Clock({template: 'h:m:s'}); class Clock { // ({ ... }) allows the item to be passed in as a object literal constructor({template}){ + //wanted to console.log(typeof({template})); this._template = template; } From 564d46149489508f538030ac2886c4c5684b49ea Mon Sep 17 00:00:00 2001 From: Alex Reany Date: Thu, 27 Apr 2017 16:15:21 -0700 Subject: [PATCH 08/11] done --- bike-shop/src/stage4-inheritance.js | 11 +++-- index.html | 5 +- jsinfo/rewriteToClass.js | 2 +- music-player/album.js | 23 ++++++++- music-player/artist.js | 19 +++++++- music-player/playlist.js | 39 +++++++++++++++- music-player/song.js | 72 ++++++++++++++++++++++++++++- 7 files changed, 160 insertions(+), 11 deletions(-) diff --git a/bike-shop/src/stage4-inheritance.js b/bike-shop/src/stage4-inheritance.js index e139f94..80db304 100644 --- a/bike-shop/src/stage4-inheritance.js +++ b/bike-shop/src/stage4-inheritance.js @@ -41,8 +41,8 @@ class BMXBike extends Bike { } class RacingBike extends Bike { - constructor(val) { - super(val); + constructor() { + super(); this.tires[0].type = 'road'; this.tires[1].type = 'road'; this.frame.style = 'racing'; @@ -57,8 +57,10 @@ class RacingBike extends Bike { -//let mountainBike = new MountainBike() -//let racingBike = new RacingBike() +let mountainBike = new MountainBike() +let bmxBike = new BMXBike() +let racingBike = new RacingBike() +let bike = new Bike @@ -67,5 +69,4 @@ module.exports = { MountainBike: MountainBike, BMXBike: BMXBike, RacingBike: RacingBike - // you'll need to export new classes here } diff --git a/index.html b/index.html index 82f542f..263a7ca 100644 --- a/index.html +++ b/index.html @@ -5,6 +5,9 @@ - + + + + diff --git a/jsinfo/rewriteToClass.js b/jsinfo/rewriteToClass.js index 68f1be7..353f170 100644 --- a/jsinfo/rewriteToClass.js +++ b/jsinfo/rewriteToClass.js @@ -41,7 +41,7 @@ let clock = new Clock({template: 'h:m:s'}); class Clock { // ({ ... }) allows the item to be passed in as a object literal constructor({template}){ - //wanted to + //wanted to check what type of console.log(typeof({template})); this._template = template; } diff --git a/music-player/album.js b/music-player/album.js index 877a3aa..1e32332 100644 --- a/music-player/album.js +++ b/music-player/album.js @@ -1 +1,22 @@ -// Your code here +//const Song = require ('./song.js') + +class Album { + + constructor(album){ + + this.artist = album[0].artist + this.title = album[0].album + this.genre = album[0].genre + this.tracks = album + this.songCount = Physical_Graffiti.length + } +} + +let newAlbum = new Album(Physical_Graffiti) + +//Artist name +let LedZeppelin = [["Jimmy Page", "John Bonham", 'Robert Plant', 'John Paul Jones']] + +LedZeppelin.push(new Album (Physical_Graffiti)) +LedZeppelin.push(new Album (Physical_Graffiti1)) +console.log(); diff --git a/music-player/artist.js b/music-player/artist.js index 877a3aa..1a9785b 100644 --- a/music-player/artist.js +++ b/music-player/artist.js @@ -1 +1,18 @@ -// Your code here +class Artist{ + + constructor(artist){ + console.log(artist); + this.artist = artist[1].artist + this.bandMembers = artist[0] + this.genre = artist[1].genre + this.albums = artist.splice(1,artist.length-1) + + + } +} + +let newArtist = new Artist(LedZeppelin) + +//PlayList +let LedZeppelinPL = [] +LedZeppelinPL.push(new Artist(LedZeppelin)) diff --git a/music-player/playlist.js b/music-player/playlist.js index 877a3aa..63dfdfa 100644 --- a/music-player/playlist.js +++ b/music-player/playlist.js @@ -1 +1,38 @@ -// Your code here +//inherit from song + +class PlayList { + + constructor(name){ + console.log(name); + this.playListName = "My Play List" + + this.songListArr = [] + } + + addSong( ) { + this.songListArr.push("some song") + console.log(this.songListArr); + console.log("Song Added "); + } + + removeSong ( ){ + let findSong = this.songListArr.indexOf("some song"); + this.songListArr.splice(findSong, 1); + console.log(this.title + " has been removed"); + } + + currentSong(){ + console.log(this.songListArr[0]); + } + + Play( ) { + this._isPlaying = true; + console.log("I am playing a song " + this.title); + } + + Pause ( ){ + this._isPlaying = false; + console.log(this.title + " is now paused"); + } +} + let newPlayList = new PlayList(newArtist) diff --git a/music-player/song.js b/music-player/song.js index 877a3aa..764221a 100644 --- a/music-player/song.js +++ b/music-player/song.js @@ -1 +1,71 @@ -// Your code here +class Song { + + constructor(song){ + + + this.title = song.title; + //in seconds + this.songLength = song.songLength; + this.album = song.album + this.artist = song.artist + this.track = song.track + this.genre = song.genre + this._isPlaying = false; + } + + Play( ) { + this._isPlaying = true; + console.log("I am playing a song " + this.title); + } + + Pause ( ){ + this._isPlaying = false; + console.log(this.title + " is now paused"); + } +} + +let kashmirLedZ = { + title: "Kashmir", + songLength: 510, + album: "Physical Graffiti", + artist: "Led Zeppelin", + track: 6, + genre: "Rock" +} + +let custardPieLedZ = { + title: "Custard Pie", + songLength: 260, + album: "Physical Graffiti", + artist: "Led Zeppelin", + track: 1, + genre: "Rock" +} + + +//Album name +let Physical_Graffiti = [] + +Physical_Graffiti.push(new Song (custardPieLedZ)) +Physical_Graffiti.push(new Song (kashmirLedZ)) + +let Physical_Graffiti1 = [] + +Physical_Graffiti1.push(new Song (custardPieLedZ)) +Physical_Graffiti1.push(new Song (kashmirLedZ)) + +//Song name +let newSong = new Song(kashmirLedZ) + +// class Album extends Song { +// +// constructor(songs) { +// super(songs[0]) +// this.tracks = songs +// this.songCount = this.tracks.length +// +// } +// +// } +// +// let newAlbum = new Album() From cf7e2d3a23e6762e555bbeff48eb944caddc220c Mon Sep 17 00:00:00 2001 From: Alex Reany Date: Thu, 27 Apr 2017 17:38:33 -0700 Subject: [PATCH 09/11] cleaning up --- .eslintrc.js | 28 +++++++++++++++++ bike-shop/src/stage1-literals.js | 11 +++---- bike-shop/src/stage2-constructors.js | 44 +++++++++++++------------- bike-shop/src/stage3-methods.js | 40 ++++++++++++------------ bike-shop/src/stage4-inheritance.js | 18 +++++------ jsinfo/createACalc.js | 6 ++-- jsinfo/createNewAccumlator.js | 2 +- jsinfo/createNewCalc.js | 8 ++--- jsinfo/errorCreatingAnInstance.js | 4 +-- jsinfo/errorInInheritance.js | 4 +-- jsinfo/extendClock.js | 17 +++++++--- jsinfo/extendableCalculator.js | 34 ++++++++++---------- jsinfo/helloObject.js | 8 ++--- jsinfo/multiplyNumericByTwo.js | 4 +-- jsinfo/rewritePrototype.js | 2 +- jsinfo/rewriteToClass.js | 4 +-- jsinfo/twoHamsters.js | 2 +- music-player/album.js | 19 ++++++------ music-player/playlist.js | 20 ++++++------ music-player/song.js | 46 ++++++++++++++-------------- package.json | 19 ++++++++++++ 21 files changed, 197 insertions(+), 143 deletions(-) create mode 100644 .eslintrc.js create mode 100644 package.json diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..4463610 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,28 @@ +module.exports = { + "env": { + "browser": true, + "es6": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "sourceType": "module" + }, + "rules": { + "indent": [ + "error", + "tab" + ], + "linebreak-style": [ + "error", + "unix" + ], + "quotes": [ + "error", + "single" + ], + "semi": [ + "error", + "always" + ] + } +}; \ No newline at end of file diff --git a/bike-shop/src/stage1-literals.js b/bike-shop/src/stage1-literals.js index e18a7e2..e02ed56 100644 --- a/bike-shop/src/stage1-literals.js +++ b/bike-shop/src/stage1-literals.js @@ -1,11 +1,10 @@ const myBike = { - - name : "Roadster", + name : 'Roadster', price : 199.99, frame : { - color : "blue", + color : 'blue', height: 55, - style : "cruiser" + style : 'cruiser' }, brakes : { back : true, @@ -13,9 +12,9 @@ const myBike = { }, tires : { diameter : 22, - type : "fat" + type : 'fat' }, rings : [2, 5] }; -module.exports = myBike +module.exports = myBike; diff --git a/bike-shop/src/stage2-constructors.js b/bike-shop/src/stage2-constructors.js index 4c26afb..73ee50b 100644 --- a/bike-shop/src/stage2-constructors.js +++ b/bike-shop/src/stage2-constructors.js @@ -1,25 +1,25 @@ function Bike(name, price) { - this.name = name - this.price = price - this.rings = [3, 7] + this.name = name; + this.price = price; + this.rings = [3, 7]; this.brakes = { back : true, front : true }, - this.tires = [new Tire, new Tire] - this.frame = new Frame + this.tires = [new Tire, new Tire]; + this.frame = new Frame; } function Frame(color, size, style) { - this.color = color ? color : "black" - this.size = size ? size : 55 - this.style = style ? style : "street" + this.color = color ? color : 'black'; + this.size = size ? size : 55; + this.style = style ? style : 'street'; } function Tire(diameter, type) { - this.diameter = diameter ? diameter : 22 - this.type = type ? type : 'street' - this.rings = [2, 5] + this.diameter = diameter ? diameter : 22; + this.type = type ? type : 'street'; + this.rings = [2, 5]; } @@ -27,21 +27,21 @@ module.exports = { Bike: Bike, Frame: Frame, Tire: Tire -} +}; -const myFrame = new Frame() -//myFrame.color = "black" +const myFrame = new Frame(); +//myFrame.color = 'black' //myFrame.size = 55 -//myFrame.style = "street" +//myFrame.style = 'street' -const myBike = new Bike() -//myBike.name = "" -//myBike.price = "" -//myBike.tires = ["front", "back"] +const myBike = new Bike(); +//myBike.name = '' +//myBike.price = '' +//myBike.tires = ['front', 'back'] -const myTire = new Tire() -//myTire.diameter = "" -//myTire.type = "" +const myTire = new Tire(); +//myTire.diameter = '' +//myTire.type = '' diff --git a/bike-shop/src/stage3-methods.js b/bike-shop/src/stage3-methods.js index e84e96a..62f055f 100644 --- a/bike-shop/src/stage3-methods.js +++ b/bike-shop/src/stage3-methods.js @@ -1,6 +1,6 @@ function Bike() { - this.rings = [3, 7] + this.rings = [3, 7]; this.brakes = { back : true, front : true @@ -8,7 +8,7 @@ function Bike() { this.tires = [new Tire, new Tire], this.frame = new Frame, - this._isMoving = false + this._isMoving = false; } function Frame() { @@ -16,52 +16,52 @@ function Frame() { } function Tire() { - this._isFlat = false + this._isFlat = false; } Tire.prototype.isFlat = function () { - return this._isFlat -} + return this._isFlat; +}; Tire.prototype.puncture = function () { - this._isFlat = true -} + this._isFlat = true; +}; Tire.prototype.repair = function () { this._isFlat = false; -} +}; Bike.prototype.isMoving = function () { - return this._isMoving -} + return this._isMoving; +}; Bike.prototype.pedal = function () { if (this.tires[0]._isFlat === true) { - throw "Can't pedal with a flat tire" + throw "Can't pedal with a flat tire"; }else { - this._isMoving = true + this._isMoving = true; } -} +}; Bike.prototype.brake = function () { - this._isMoving = false -} + this._isMoving = false; +}; Bike.prototype.gearSpeeds = function () { - return this.rings[0] * this.rings[1] -} + return this.rings[0] * this.rings[1]; +}; -const myTire = new Tire() -const myBike = new Bike() +const myTire = new Tire(); +const myBike = new Bike(); module.exports = { Bike: Bike, Frame: Frame, Tire: Tire -} +}; diff --git a/bike-shop/src/stage4-inheritance.js b/bike-shop/src/stage4-inheritance.js index 80db304..ccb5e6c 100644 --- a/bike-shop/src/stage4-inheritance.js +++ b/bike-shop/src/stage4-inheritance.js @@ -8,14 +8,14 @@ class Tire { class Bike { constructor() { - this.tires = [new Tire, new Tire] - this.frame = new Frame + this.tires = [new Tire, new Tire]; + this.frame = new Frame; this.brakes = { back : true, front : true - } + }; this.rings = [3, 7] - } + }; } class MountainBike extends Bike { @@ -57,10 +57,10 @@ class RacingBike extends Bike { -let mountainBike = new MountainBike() -let bmxBike = new BMXBike() -let racingBike = new RacingBike() -let bike = new Bike +let mountainBike = new MountainBike(); +let bmxBike = new BMXBike(); +let racingBike = new RacingBike(); +let bike = new Bike(); @@ -69,4 +69,4 @@ module.exports = { MountainBike: MountainBike, BMXBike: BMXBike, RacingBike: RacingBike -} +}; diff --git a/jsinfo/createACalc.js b/jsinfo/createACalc.js index d852b06..17e1698 100644 --- a/jsinfo/createACalc.js +++ b/jsinfo/createACalc.js @@ -3,8 +3,8 @@ let calculator = { read() { // get values - this.value1 = parseFloat(prompt("what is the first value")); - this.value2 = parseFloat(prompt("what is the second value")); + this.value1 = parseFloat(prompt('what is the first value')); + this.value2 = parseFloat(prompt('what is the second value')); }, sum() { @@ -14,7 +14,7 @@ let calculator = { mul() { return this.value1 * this.value2; } -} +}; calculator.read(); alert( calculator.sum() ); diff --git a/jsinfo/createNewAccumlator.js b/jsinfo/createNewAccumlator.js index dceab73..fd56bca 100644 --- a/jsinfo/createNewAccumlator.js +++ b/jsinfo/createNewAccumlator.js @@ -14,7 +14,7 @@ function Accumulator(startingValue) { this.read = function () { //adds the prompted amount to the value at the moment - this.value += parseFloat(prompt("what is the first value")); + this.value += parseFloat(prompt('what is the first value')); }; } diff --git a/jsinfo/createNewCalc.js b/jsinfo/createNewCalc.js index a3922fe..d2c871f 100644 --- a/jsinfo/createNewCalc.js +++ b/jsinfo/createNewCalc.js @@ -6,8 +6,8 @@ function Calculator() { this.read = function () { // get values - this.value1 = parseFloat(prompt("what is the first value")); - this.value2 = parseFloat(prompt("what is the second value")); + this.value1 = parseFloat(prompt('what is the first value')); + this.value2 = parseFloat(prompt('what is the second value')); }; this.sum = function () { @@ -26,5 +26,5 @@ let calculator = new Calculator(); calculator.read(); -alert( "Sum=" + calculator.sum() ); -alert( "Mul=" + calculator.mul() ); +alert( 'Sum=' + calculator.sum() ); +alert( 'Mul=' + calculator.mul() ); diff --git a/jsinfo/errorCreatingAnInstance.js b/jsinfo/errorCreatingAnInstance.js index 9a2eb28..596a6e9 100644 --- a/jsinfo/errorCreatingAnInstance.js +++ b/jsinfo/errorCreatingAnInstance.js @@ -15,12 +15,12 @@ class Animal { class Rabbit extends Animal { constructor(name) { //need to add a supper function to allow inheritance - super(name) + super(name); //now with the super() //this.name = name; this.created = Date.now(); } } -let rabbit = new Rabbit("White Rabbit"); // Error: this is not defined +let rabbit = new Rabbit('White Rabbit'); // Error: this is not defined alert(rabbit.name); diff --git a/jsinfo/errorInInheritance.js b/jsinfo/errorInInheritance.js index 589f8d2..c8e8958 100644 --- a/jsinfo/errorInInheritance.js +++ b/jsinfo/errorInInheritance.js @@ -20,8 +20,8 @@ Rabbit.prototype = Animal.prototype; //This will overwrite the previous walk prototype and animals will now bounce and not walk Rabbit.prototype.walk = function() { - alert(this.name + " bounces!"); + alert(this.name + ' bounces!'); }; let rabbit = new Rabbit; -let animal = new Animal("skunk"); +let animal = new Animal('skunk'); diff --git a/jsinfo/extendClock.js b/jsinfo/extendClock.js index 238148c..17b1cf8 100644 --- a/jsinfo/extendClock.js +++ b/jsinfo/extendClock.js @@ -7,7 +7,7 @@ class Clock { _render() { let date = new Date(); - let hours = date.getHours() + let hours = date.getHours(); if (hours < 10) hours = '0' + hours; let mins = date.getMinutes(); @@ -27,14 +27,23 @@ class Clock { //clock.stop() will stop the clock stop() { clearInterval(this._timer); - console.log("clocked stopped"); + console.log('clocked stopped'); }; //clock.start() would then restart the clock start() { this._render(); - //setInterval(function, milliseconds, param1, param2, ...) + //setInterval(function, milliseconds, param1, param2, ...) this._timer = setInterval(() => this._render(), this._precision); + + //this._timer = setInterval(this._render, this._precision); + + var myfunc = () => this._render(); + setInterval(myfunc, 5000); + + this._timer = setInterval(function() { + return this._render(); + }, this._precision); } } @@ -43,7 +52,7 @@ class ExtendedClock extends Clock { constructor(val) { //allows for the inheritence to be manipulated - super(val) + super(val); //set the variable let {precision=1000} = val; this._precision = precision; diff --git a/jsinfo/extendableCalculator.js b/jsinfo/extendableCalculator.js index 7695075..0962ef7 100644 --- a/jsinfo/extendableCalculator.js +++ b/jsinfo/extendableCalculator.js @@ -3,40 +3,40 @@ function Calculator() { //holds new method funtions let methods = { - "+" : function(value1, value2){ - return parseFloat(value1) + parseFloat(value2) + '+' : function(value1, value2){ + return parseFloat(value1) + parseFloat(value2); }, //arrow function - "-" : (value1, value2) => parseFloat(value1) - parseFloat(value2), - } + '-' : (value1, value2) => parseFloat(value1) - parseFloat(value2), + }; this.calculate = function (str) { - let strSplit = str.split(" "); + let strSplit = str.split(' '); var value1 = strSplit[0]; var value2 = strSplit[2]; var operator = strSplit[1]; //return the value of the string to be operated on - return methods[operator](value1, value2) + return methods[operator](value1, value2); }; this.addOperator =function (name, func){ methods[name] = func; - } +}; } let calc = new Calculator; -alert( calc.calculate("3 + 7") ); // 10 +alert( calc.calculate('3 + 7') ); // 10 -let newCalc = new Calculator +let newCalc = new Calculator; -newCalc.addOperator("*", (a, b) => a * b); -newCalc.addOperator("/", (a, b) => a / b); -newCalc.addOperator("**", (a, b) => a ** b) -newCalc.addOperator("pie", (a,b) => "I have " + a + " pies cut into " + b + " slices each") -alert(newCalc.calculate("2 * 3")); //6 -alert(newCalc.calculate("2 ** 3")); //8 -alert(newCalc.calculate("6 / 2")); //3 -alert(newCalc.calculate("7 pie 4")); +newCalc.addOperator('*', (a, b) => a * b); +newCalc.addOperator('/', (a, b) => a / b); +newCalc.addOperator('**', (a, b) => a ** b) +newCalc.addOperator('pie', (a,b) => 'I have ' + a + ' pies cut into ' + b + ' slices each'); +alert(newCalc.calculate('2 * 3')); //6 +alert(newCalc.calculate('2 ** 3')); //8 +alert(newCalc.calculate('6 / 2')); //3 +alert(newCalc.calculate('7 pie 4')); diff --git a/jsinfo/helloObject.js b/jsinfo/helloObject.js index 4c037f7..054b976 100644 --- a/jsinfo/helloObject.js +++ b/jsinfo/helloObject.js @@ -5,11 +5,11 @@ let user = { - name : "John", - surname : "Smith" + name : 'John', + surname : 'Smith' }; -// reassign name to "Pete" +// reassign name to 'Pete' -user.name = "Pete"; +user.name = 'Pete'; // Delete the property of name delete user.name; diff --git a/jsinfo/multiplyNumericByTwo.js b/jsinfo/multiplyNumericByTwo.js index fd901ce..08c5a6b 100644 --- a/jsinfo/multiplyNumericByTwo.js +++ b/jsinfo/multiplyNumericByTwo.js @@ -16,8 +16,8 @@ let menu = { width: 200, height: 300, - title: "My menu" + title: 'My menu' }; //call the function - multiplyNumeric(menu) + multiplyNumeric(menu); diff --git a/jsinfo/rewritePrototype.js b/jsinfo/rewritePrototype.js index e83108c..6197c66 100644 --- a/jsinfo/rewritePrototype.js +++ b/jsinfo/rewritePrototype.js @@ -62,7 +62,7 @@ Clock.prototype.render = function(){ .replace('s', secs); console.log(output); -} +}; Clock.prototype.stop = function() { clearInterval(this.timer); diff --git a/jsinfo/rewriteToClass.js b/jsinfo/rewriteToClass.js index 353f170..6b90ea3 100644 --- a/jsinfo/rewriteToClass.js +++ b/jsinfo/rewriteToClass.js @@ -49,7 +49,7 @@ class Clock { _render() { let date = new Date(); - let hours = date.getHours() + let hours = date.getHours(); if (hours < 10) hours = '0' + hours; let mins = date.getMinutes(); @@ -69,7 +69,7 @@ class Clock { //clock.stop() will stop the clock stop() { clearInterval(this._timer); - }; + } //clock.start() would then restart the clock start() { diff --git a/jsinfo/twoHamsters.js b/jsinfo/twoHamsters.js index 6703176..0a6509a 100644 --- a/jsinfo/twoHamsters.js +++ b/jsinfo/twoHamsters.js @@ -23,7 +23,7 @@ let lazy = { }; // This one found the food -speedy.eat("apple"); +speedy.eat('apple'); alert( speedy.stomach ); // apple // This one also has it, why? fix please. diff --git a/music-player/album.js b/music-player/album.js index 1e32332..1876e63 100644 --- a/music-player/album.js +++ b/music-player/album.js @@ -4,19 +4,18 @@ class Album { constructor(album){ - this.artist = album[0].artist - this.title = album[0].album - this.genre = album[0].genre - this.tracks = album - this.songCount = Physical_Graffiti.length + this.artist = album[0].artist; + this.title = album[0].album; + this.genre = album[0].genre; + this.tracks = album; + this.songCount = Physical_Graffiti.length; } } -let newAlbum = new Album(Physical_Graffiti) +let newAlbum = new Album(Physical_Graffiti); //Artist name -let LedZeppelin = [["Jimmy Page", "John Bonham", 'Robert Plant', 'John Paul Jones']] +let LedZeppelin = [["Jimmy Page", "John Bonham", 'Robert Plant', 'John Paul Jones']]; -LedZeppelin.push(new Album (Physical_Graffiti)) -LedZeppelin.push(new Album (Physical_Graffiti1)) -console.log(); +LedZeppelin.push(new Album (Physical_Graffiti)); +LedZeppelin.push(new Album (Physical_Graffiti1)); diff --git a/music-player/playlist.js b/music-player/playlist.js index 63dfdfa..d470945 100644 --- a/music-player/playlist.js +++ b/music-player/playlist.js @@ -4,35 +4,35 @@ class PlayList { constructor(name){ console.log(name); - this.playListName = "My Play List" + this.playListName = 'My Play List'; - this.songListArr = [] + this.songListArr = []; } addSong( ) { - this.songListArr.push("some song") + this.songListArr.push('some song'); console.log(this.songListArr); - console.log("Song Added "); + console.log('Song Added '); } removeSong ( ){ - let findSong = this.songListArr.indexOf("some song"); + let findSong = this.songListArr.indexOf('some song'); this.songListArr.splice(findSong, 1); - console.log(this.title + " has been removed"); + console.log(this.title + ' has been removed'); } currentSong(){ console.log(this.songListArr[0]); } - + Play( ) { this._isPlaying = true; - console.log("I am playing a song " + this.title); + console.log('I am playing a song ' + this.title); } Pause ( ){ this._isPlaying = false; - console.log(this.title + " is now paused"); + console.log(this.title + ' is now paused'); } } - let newPlayList = new PlayList(newArtist) + let newPlayList = new PlayList(newArtist); diff --git a/music-player/song.js b/music-player/song.js index 764221a..27079c4 100644 --- a/music-player/song.js +++ b/music-player/song.js @@ -6,56 +6,56 @@ class Song { this.title = song.title; //in seconds this.songLength = song.songLength; - this.album = song.album - this.artist = song.artist - this.track = song.track - this.genre = song.genre + this.album = song.album; + this.artist = song.artist; + this.track = song.track; + this.genre = song.genre; this._isPlaying = false; } Play( ) { this._isPlaying = true; - console.log("I am playing a song " + this.title); + console.log('I am playing a song ' + this.title); } Pause ( ){ this._isPlaying = false; - console.log(this.title + " is now paused"); + console.log(this.title + ' is now paused'); } } let kashmirLedZ = { - title: "Kashmir", + title: 'Kashmir', songLength: 510, - album: "Physical Graffiti", - artist: "Led Zeppelin", + album: 'Physical Graffiti', + artist: 'Led Zeppelin', track: 6, - genre: "Rock" -} + genre: 'Rock' +}; let custardPieLedZ = { - title: "Custard Pie", + title: 'Custard Pie', songLength: 260, - album: "Physical Graffiti", - artist: "Led Zeppelin", + album: 'Physical Graffiti', + artist: 'Led Zeppelin', track: 1, - genre: "Rock" -} + genre: 'Rock' +}; //Album name -let Physical_Graffiti = [] +let Physical_Graffiti = []; -Physical_Graffiti.push(new Song (custardPieLedZ)) -Physical_Graffiti.push(new Song (kashmirLedZ)) +Physical_Graffiti.push(new Song (custardPieLedZ)); +Physical_Graffiti.push(new Song (kashmirLedZ)); -let Physical_Graffiti1 = [] +let Physical_Graffiti1 = []; -Physical_Graffiti1.push(new Song (custardPieLedZ)) -Physical_Graffiti1.push(new Song (kashmirLedZ)) +Physical_Graffiti1.push(new Song (custardPieLedZ)); +Physical_Graffiti1.push(new Song (kashmirLedZ)); //Song name -let newSong = new Song(kashmirLedZ) +let newSong = new Song(kashmirLedZ); // class Album extends Song { // diff --git a/package.json b/package.json new file mode 100644 index 0000000..a2894d4 --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "oop-practice", + "version": "1.0.0", + "description": "A place to practice object-oriented programming (OOP) in JavaScript.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/ReanyAlex/oop-practice.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/ReanyAlex/oop-practice/issues" + }, + "homepage": "https://github.com/ReanyAlex/oop-practice#readme" +} From 75836651199c27a3440464dc63ed5da04f488d42 Mon Sep 17 00:00:00 2001 From: Alex Reany Date: Thu, 27 Apr 2017 17:39:29 -0700 Subject: [PATCH 10/11] cleaning up --- music-player/album.js | 2 +- music-player/artist.js | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/music-player/album.js b/music-player/album.js index 1876e63..972902e 100644 --- a/music-player/album.js +++ b/music-player/album.js @@ -15,7 +15,7 @@ class Album { let newAlbum = new Album(Physical_Graffiti); //Artist name -let LedZeppelin = [["Jimmy Page", "John Bonham", 'Robert Plant', 'John Paul Jones']]; +let LedZeppelin = [['Jimmy Page', 'John Bonham', 'Robert Plant', 'John Paul Jones']]; LedZeppelin.push(new Album (Physical_Graffiti)); LedZeppelin.push(new Album (Physical_Graffiti1)); diff --git a/music-player/artist.js b/music-player/artist.js index 1a9785b..f13950e 100644 --- a/music-player/artist.js +++ b/music-player/artist.js @@ -2,17 +2,17 @@ class Artist{ constructor(artist){ console.log(artist); - this.artist = artist[1].artist - this.bandMembers = artist[0] - this.genre = artist[1].genre - this.albums = artist.splice(1,artist.length-1) + this.artist = artist[1].artist; + this.bandMembers = artist[0]; + this.genre = artist[1].genre; + this.albums = artist.splice(1,artist.length-1); } } -let newArtist = new Artist(LedZeppelin) +let newArtist = new Artist(LedZeppelin); //PlayList -let LedZeppelinPL = [] -LedZeppelinPL.push(new Artist(LedZeppelin)) +let LedZeppelinPL = []; +LedZeppelinPL.push(new Artist(LedZeppelin)); From acae15d6a1fdaa74f0868f92eb009ee5661137ca Mon Sep 17 00:00:00 2001 From: Alex Reany Date: Fri, 28 Apr 2017 11:22:16 -0700 Subject: [PATCH 11/11] rewrote our music player so that classes wouldnt be depent on multiple other classes --- .eslintrc.js | 28 ------------------ index.html | 9 +++--- music-player/albumNew.js | 23 +++++++++++++++ music-player/artistNew.js | 14 +++++++++ music-player/playerNew.js | 57 +++++++++++++++++++++++++++++++++++++ music-player/playlistNew.js | 40 ++++++++++++++++++++++++++ music-player/songNew.js | 23 +++++++++++++++ 7 files changed, 162 insertions(+), 32 deletions(-) delete mode 100644 .eslintrc.js create mode 100644 music-player/albumNew.js create mode 100644 music-player/artistNew.js create mode 100644 music-player/playerNew.js create mode 100644 music-player/playlistNew.js create mode 100644 music-player/songNew.js diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 4463610..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1,28 +0,0 @@ -module.exports = { - "env": { - "browser": true, - "es6": true - }, - "extends": "eslint:recommended", - "parserOptions": { - "sourceType": "module" - }, - "rules": { - "indent": [ - "error", - "tab" - ], - "linebreak-style": [ - "error", - "unix" - ], - "quotes": [ - "error", - "single" - ], - "semi": [ - "error", - "always" - ] - } -}; \ No newline at end of file diff --git a/index.html b/index.html index 263a7ca..9ab7b0f 100644 --- a/index.html +++ b/index.html @@ -5,9 +5,10 @@ - - - - + + + + + diff --git a/music-player/albumNew.js b/music-player/albumNew.js new file mode 100644 index 0000000..0d3565b --- /dev/null +++ b/music-player/albumNew.js @@ -0,0 +1,23 @@ +//const Song = require ('./song.js') + +class Album { +//deconstruct object + constructor({artist, title, genre, songs, numOfTracks}){ + + this.artist = artist + this.title = title; + this.genre = genre; + this.songs = songs; + this.numOfTracks = numOfTracks; + //check later + this.songCount = songs.length + } +} + +// albumObj ={ +// artist: +// title: +// genre: +// songs: [] +// numOfTracks: +// } diff --git a/music-player/artistNew.js b/music-player/artistNew.js new file mode 100644 index 0000000..d00dc0b --- /dev/null +++ b/music-player/artistNew.js @@ -0,0 +1,14 @@ +class Artist{ + + constructor(artist){ + this.name = artist.name; + this.bandMembers = artist.bandMembers; + this.genre = artist.genre; + } +} + +// artistObj= { +// name: +// bandMembers: +// genre: +// } diff --git a/music-player/playerNew.js b/music-player/playerNew.js new file mode 100644 index 0000000..a04a03d --- /dev/null +++ b/music-player/playerNew.js @@ -0,0 +1,57 @@ +class Player { + constructor(name){ + this.name = name + //we changed check back + this.songs = []; + + } +} + +//Artist +let lZep = { + name: 'Led Zeppelin', + bandMembers: ['Jimmy Page', 'John Bonham', 'Robert Plant', 'John Paul Jones'], + genre: 'Rock' +}; + +const ledZep = new Artist(lZep); + +//Album +let ledZepAlb ={ + artist: 'Led Zeppelin', + title: 'Physical Graffiti', + genre: 'rock', + songs: [], + numOfTracks: 2 +}; + +const phyGra = new Album(ledZepAlb); + +//Songs + +let kashmirLedZ = { + title: 'Kashmir', + songLength: 510, + album: 'Physical Graffiti', + artist: 'Led Zeppelin', + track: 6, + genre: 'Rock' +}; + +let custardPieLedZ = { + title: 'Custard Pie', + songLength: 260, + album: 'Physical Graffiti', + artist: 'Led Zeppelin', + track: 1, + genre: 'Rock' +}; + +phyGra.songs = [ + new Song(kashmirLedZ), + new Song(custardPieLedZ) +]; + +//PlayerList + +let newPlayList = new PlayList("Our Play List"); diff --git a/music-player/playlistNew.js b/music-player/playlistNew.js new file mode 100644 index 0000000..8119ea5 --- /dev/null +++ b/music-player/playlistNew.js @@ -0,0 +1,40 @@ +//inherit from song + +class PlayList { + + constructor(name){ + this.playListName = name; + this.songListArr = []; + this._listLength = 0 + } + + addSong(song) { + this.songListArr.push(song); + + console.log('Song Added ' + song.title); + } + + removeSong (song){ + for (var i = 0; i < this.songListArr.length; i++) { + if (this.songListArr[i].title === song) { + console.log(this.songListArr[i].title + ' has been removed'); + this.songListArr.splice(i, 1); + break; + }; + }; + } + + currentSong(){ + console.log("Information about current song " + this.songListArr[0]); + } + + play( ) { + this._isPlaying = true; + console.log('I am playing a song ' + this.songListArr[0].title); + } + + pause ( ){ + this._isPlaying = false; + console.log(this.songListArr[0].title + ' is now paused'); + } +} diff --git a/music-player/songNew.js b/music-player/songNew.js new file mode 100644 index 0000000..9601d41 --- /dev/null +++ b/music-player/songNew.js @@ -0,0 +1,23 @@ +class Song { +//agrument passed as an object + constructor(song){ + this.title = song.title; + //in seconds + this.songLength = song.songLength; + this.album = song.album; + this.artist = song.artist; + this.track = song.track; + this.genre = song.genre; + this._isPlaying = false; + } + + Play( ) { + this._isPlaying = true; + console.log('I am playing a song ' + this.title); + } + + Pause ( ){ + this._isPlaying = false; + console.log(this.title + ' is now paused'); + } +}