Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bike-shop/src/stage2-constructors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
function Bike(name, price) {
alert(new.target);
this.name = name
this.price = price
this.rings = [3, 7]
Expand Down
55 changes: 53 additions & 2 deletions bike-shop/src/stage3-methods.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,66 @@
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() {
// 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,
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
<title></title>
</head>
<body>
<script src="bike-shop/src/stage2-constructors.js" charset="UTF-8"></script>
<script src="bike-shop/src/stage3-methods.js" charset="UTF-8"></script>
</body>
</html>
1 change: 1 addition & 0 deletions jsinfo/createACalc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
},
Expand Down
1 change: 1 addition & 0 deletions jsinfo/createNewCalc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
};
Expand Down
27 changes: 27 additions & 0 deletions jsinfo/errorInInheritance.js
Original file line number Diff line number Diff line change
@@ -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");
2 changes: 2 additions & 0 deletions jsinfo/extendableCalculator.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
};

Expand Down
79 changes: 79 additions & 0 deletions jsinfo/rewritePrototype.js
Original file line number Diff line number Diff line change
@@ -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();
81 changes: 81 additions & 0 deletions jsinfo/rewriteToClass.js
Original file line number Diff line number Diff line change
@@ -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();
15 changes: 12 additions & 3 deletions jsinfo/searthingAlgo.js → jsinfo/searchingAlgo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions jsinfo/twoHamsters.js
Original file line number Diff line number Diff line change
@@ -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