diff --git a/bike-shop/src/stage1-literals.js b/bike-shop/src/stage1-literals.js index c749dd2..100cde0 100644 --- a/bike-shop/src/stage1-literals.js +++ b/bike-shop/src/stage1-literals.js @@ -1,5 +1,20 @@ const myBike = { - // your code here + name: "Roadster", + price: 199.99, + frame: { + height: 55, + color: 'blue', + style: 'cruiser' + }, + brakes: { + front: false, + back: true + }, + 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..17c520d 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.tires = [new Tire(), new Tire()], + this.frame = new Frame(), + this.rings = [3, 7], + this.brakes = { + front: true, + back: true + } } -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/An_error_in_the_inheritance.js b/jsinfo/An_error_in_the_inheritance.js new file mode 100644 index 0000000..a2a838c --- /dev/null +++ b/jsinfo/An_error_in_the_inheritance.js @@ -0,0 +1,20 @@ +function Animal(name) { + this.name = name; +} + +Animal.prototype.walk = function() { + console.log(this.name + ' walks'); +}; + +function Rabbit(name) { + this.name = name; +} + +Rabbit.prototype.__proto__ = Animal.prototype; + +Rabbit.prototype.walk = function() { + console.log(this.name + " bounces!"); +}; + +let rabbit = new Rabbit('Bootsie') +console.log(rabbit.walk()) diff --git a/jsinfo/Chaining.js b/jsinfo/Chaining.js new file mode 100644 index 0000000..063a166 --- /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/Create_a_calculator.js b/jsinfo/Create_a_calculator.js new file mode 100644 index 0000000..a80d3ba --- /dev/null +++ b/jsinfo/Create_a_calculator.js @@ -0,0 +1,19 @@ +let calculator = { + + sum() { + return this.a + this.b + }, + + mul() { + return this.a * this.b + }, + + read() { + this.a = +prompt('Please enter your first number:') + this.b = +prompt('Please enter your second number:') + }, +}; + +calculator.read(); +alert('The sum is: ' + calculator.sum()); +alert('The product is: ' + calculator.mul()); diff --git a/jsinfo/Create_an_extendable_calculator.js b/jsinfo/Create_an_extendable_calculator.js new file mode 100644 index 0000000..e06a806 --- /dev/null +++ b/jsinfo/Create_an_extendable_calculator.js @@ -0,0 +1,26 @@ +function Calculator() { + this.calculate = function(str) { + let arr = str.split(' ') + this.a = parseInt(arr[0]) + this.op = arr[1] + this.b = parseInt(arr[2]) + return this.operands[this.op](this.a, this.b) + }, + + this.addMethod = function(name, func) { + this.operands[name] = func + }, + + this.operands = { + '+': (a, b) => a + b, + '-': (a, b) => a - b, + } +} + +let powerCalc = new Calculator(); +powerCalc.addMethod("*", (a, b) => a * b); +powerCalc.addMethod("/", (a, b) => a / b); +powerCalc.addMethod("**", (a, b) => a ** b); + +let result = powerCalc.calculate("2 / 4"); +console.log(result); diff --git a/jsinfo/Create_new_accumulator.js b/jsinfo/Create_new_accumulator.js new file mode 100644 index 0000000..2b99247 --- /dev/null +++ b/jsinfo/Create_new_accumulator.js @@ -0,0 +1,12 @@ +function Accumulator(startingValue) { + this.value = startingValue; + + this.read = function() { + this.value += +prompt('Please enter a value:') + } +} + +let accumulator = new Accumulator(1); +accumulator.read(); +accumulator.read(); +alert(accumulator.value); diff --git a/jsinfo/Create_new_calculator.js b/jsinfo/Create_new_calculator.js new file mode 100644 index 0000000..1904f7d --- /dev/null +++ b/jsinfo/Create_new_calculator.js @@ -0,0 +1,20 @@ +function Calculator() { + this.sum = function() { + return this.a + this.b + }, + + this.mul = function() { + return this.a * this.b + }, + + this.read = function() { + this.a = +prompt('Please enter your first number:') + this.b = +prompt('Please enter your second number:') + } +}; + +let calculator = new Calculator(); +calculator.read(); + +alert('The sum is: ' + calculator.sum()); +alert('The product is: ' + calculator.mul()); diff --git a/jsinfo/Hello_object.js b/jsinfo/Hello_object.js new file mode 100644 index 0000000..e0bca5a --- /dev/null +++ b/jsinfo/Hello_object.js @@ -0,0 +1,8 @@ +let user = {} + +user.name = 'John' +user.surname = 'Smith' + +user.name = 'Pete' + +delete user.name diff --git a/jsinfo/Multiply_numeric_properties_by_2.js b/jsinfo/Multiply_numeric_properties_by_2.js new file mode 100644 index 0000000..7515ae0 --- /dev/null +++ b/jsinfo/Multiply_numeric_properties_by_2.js @@ -0,0 +1,15 @@ +let menu = { + width: 200, + height: 300, + title: 'My menu' +}; + +function multiplyNumeric(obj) { + for(let key in obj) { + if(typeof obj[key] === 'number') { + obj[key] *= 2; + } + } +} + +multiplyNumeric(menu) diff --git a/jsinfo/Rewrite_to_prototypes.js b/jsinfo/Rewrite_to_prototypes.js new file mode 100644 index 0000000..7a1f01b --- /dev/null +++ b/jsinfo/Rewrite_to_prototypes.js @@ -0,0 +1,34 @@ +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); + }; diff --git a/jsinfo/Searching_algorithm.js b/jsinfo/Searching_algorithm.js new file mode 100644 index 0000000..32a6b75 --- /dev/null +++ b/jsinfo/Searching_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/jsinfo/Why_two_hamsters_are_full.js b/jsinfo/Why_two_hamsters_are_full.js new file mode 100644 index 0000000..bb38cd5 --- /dev/null +++ b/jsinfo/Why_two_hamsters_are_full.js @@ -0,0 +1,23 @@ +let hamster = { + stomach: [], + + eat(food) { + this.stomach.push(food); + }, + +}; + +let speedy = { + __proto__: hamster, + stomach: [] +}; + +let lazy = { + __proto__: hamster, + stomach: [] +}; + +speedy.eat("apple") +console.log('Speedy: ', speedy.stomach ); + +console.log('Lazy: ', lazy.stomach );