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/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/Create_calculator.js b/jsinfo/Create_calculator.js new file mode 100644 index 0000000..ae1ae47 --- /dev/null +++ b/jsinfo/Create_calculator.js @@ -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/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/Hello_Object.js b/jsinfo/Hello_Object.js new file mode 100644 index 0000000..4e53226 --- /dev/null +++ b/jsinfo/Hello_Object.js @@ -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.js b/jsinfo/Ladder.js new file mode 100644 index 0000000..a7be386 --- /dev/null +++ b/jsinfo/Ladder.js @@ -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.js b/jsinfo/Multipy_numeric_properties.js new file mode 100644 index 0000000..380196f --- /dev/null +++ b/jsinfo/Multipy_numeric_properties.js @@ -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" +}; 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() );