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/bike-shop/src/stage2-constructors.js b/bike-shop/src/stage2-constructors.js
index c422386..4c26afb 100644
--- a/bike-shop/src/stage2-constructors.js
+++ b/bike-shop/src/stage2-constructors.js
@@ -1,17 +1,47 @@
-function Bike() {
- // your code here
+function Bike(name, price) {
+ 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/bike-shop/src/stage3-methods.js b/bike-shop/src/stage3-methods.js
index c422386..2f2fc5a 100644
--- a/bike-shop/src/stage3-methods.js
+++ b/bike-shop/src/stage3-methods.js
@@ -1,5 +1,14 @@
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() {
@@ -7,9 +16,51 @@ function Frame() {
}
function Tire() {
- // your code here
+ this._isFlat = false
}
+
+
+
+Tire.prototype.isFlat = function () {
+ return this._isFlat
+}
+
+Tire.prototype.puncture = function () {
+ this._isFlat = true
+ //this.tires[0]._isFlat = true
+}
+
+
+Tire.prototype.repair = function () {
+ this._isFlat = false;
+}
+
+Bike.prototype.isMoving = function () {
+ 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()
+
module.exports = {
Bike: Bike,
Frame: Frame,
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..2223032
--- /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..4a7e52d
--- /dev/null
+++ b/jsinfo/chaining.js
@@ -0,0 +1,45 @@
+// 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..d852b06
--- /dev/null
+++ b/jsinfo/createACalc.js
@@ -0,0 +1,21 @@
+// Create an object calculator with three methods: read , sum, mul
+
+let calculator = {
+ read() {
+ // get values
+ 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/createNewAccumlator.js b/jsinfo/createNewAccumlator.js
new file mode 100644
index 0000000..dceab73
--- /dev/null
+++ b/jsinfo/createNewAccumlator.js
@@ -0,0 +1,25 @@
+/*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) {
+ //holds the startingValue to be minipulated
+ this.value = startingValue;
+
+ this.read = function () {
+ //adds the prompted amount to the value at the moment
+ this.value += parseFloat(prompt("what is the first value"));
+ };
+}
+
+
+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..a3922fe
--- /dev/null
+++ b/jsinfo/createNewCalc.js
@@ -0,0 +1,30 @@
+/*Create a constructor function Calculator
+that creates objects with 3 methods:
+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"));
+ };
+
+ 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() );
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
new file mode 100644
index 0000000..7695075
--- /dev/null
+++ b/jsinfo/extendableCalculator.js
@@ -0,0 +1,42 @@
+//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)
+ },
+ //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 the value of the string to be operated on
+ 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/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)
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/searchingAlgo.js b/jsinfo/searchingAlgo.js
new file mode 100644
index 0000000..7e9c08b
--- /dev/null
+++ b/jsinfo/searchingAlgo.js
@@ -0,0 +1,31 @@
+/*
+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,
+ __proto__ : head
+};
+
+let bed = {
+ sheet: 1,
+ pillow: 2,
+ __proto__ : table
+};
+
+let pockets = {
+ 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