Skip to content
Open
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
Binary file added bike-shop/.DS_Store
Binary file not shown.
16 changes: 16 additions & 0 deletions bike-shop/src/stage1-literals.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
const myBike = {
// your code here
name: "Roadster",
price: 199.99,
frame: {
height: 55,
color: "blue",
style: "cruiser",
},
brakes: {
back: true,
front: false,
},
tires: {
diameter: 22,
type: "fat",
},
rings: [2,5],
}

module.exports = myBike
23 changes: 17 additions & 6 deletions bike-shop/src/stage2-constructors.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
function Bike() {
// your code here
function Bike(name, price) {
this.name= name;
this.price= price;
this.frame = new Frame();
this.rings = [3, 7];
this.brakes = {
front: true,
back: true,
};
this.tires = [new Tire(), new Tire()];
}

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 = {
Expand Down
37 changes: 35 additions & 2 deletions bike-shop/src/stage3-methods.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
function Bike() {
// your code here
this._isMoving = false;
this.rings = [3,7];
this.tires = [new Tire(), new Tire()];
}

Bike.prototype.isMoving = function () {
return this._isMoving;
}
Bike.prototype.pedal = function() {
this._isMoving = true;
if (this.tires[0]._isFlat || this.tires[1]._isFlat) {
throw("Can't pedal with a flat tire!");
}

Bike.prototype.brake = function() {
this._isMoving = false;
}

Bike.prototype.gearSpeeds = function() {
return parseInt(this.rings[0]) * parseInt(this.rings[1]);
}

};

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;
}

Tire.prototype.repair = function() {
this._isFlat = false;
}

module.exports = {
Expand Down
57 changes: 54 additions & 3 deletions bike-shop/src/stage4-inheritance.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,67 @@
class Frame {
// your code here
constructor(style){
this.style = style;
}
}

class Tire {
// your code here
constructor(diameter, type) {
this.diameter = diameter;
this.type = type;
}
}

class Bike {
// your code here
constructor() {
this.tires = [new Tire(), new Tire()];
this.frame = new Frame();
}
}

class MountainBike extends Bike {
constructor() {
super();
this.frame.style = 'mountain';
this.tires[0].type = 'dirt';
this.tires[1].type = 'dirt';
this.shocks = 20;
}
adjustShocks(x) {
this.shocks = x;
}
}

class BMXBike extends Bike {
constructor() {
super();
this.brakes = {
front : false,
back : true
}
this.tires.diameter[0] = 20;
this.tires.diameter[1] = 20;
}
}

class RacingBike extends Bike {
constructor() {
super();
this.frame.style = 'racing';
this.tires[0].type = 'road';
this.tires[1].type = 'road';
this.rings = [3, 10];
}
gearSpeeds() {
return (parseInt(this.rings[0])) * (parseInt(this.rings[1]));
}
}

module.exports = {
Bike: Bike,
// you'll need to export new classes here
Frame: Frame,
Tire: Tire,
MountainBike: MountainBike,
BMXBike: BMXBike,
RacingBike: RacingBike,
}
13 changes: 13 additions & 0 deletions jsinfo/Error_instance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Animal {

constructor(name) {
this.name = name;
}
}

class Rabbit extends Animal {
constructor(name) {
super(name);
this.created = Date.now();
}
}
5 changes: 5 additions & 0 deletions jsinfo/Hello_object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let user = {}
user.name = "John";
user.surname = "Smith";
user.name = "Pete";
delete user.name;
7 changes: 7 additions & 0 deletions jsinfo/Multiply_by_2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function multiplyNumeric(obj) {
for (let key in obj) {
if (typeof obj[key] == 'number') {
obj[key] *= 2;
}
}
}
15 changes: 15 additions & 0 deletions jsinfo/chaining.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
21 changes: 21 additions & 0 deletions jsinfo/createCalc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let calculator = {
a;
b;
read() {
this.a = +prompt("number?");
this.b = +prompt("2nd number?");

},

sum() {
return this.a + this.b;

},

mul() {
return this.a * this.b;


}

};
7 changes: 7 additions & 0 deletions jsinfo/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Rabbit.prototype = Animal.prototype

Both become the same object so the methods inside each function become jumbled.
In this case, Rabbit.prototype would override Animal.prototype so Animal.prototype inherits the function of Rabbit.prototype.

Rabbit.prototype._proto = Animal.prototype allows the prototypes to be separate and store methods of the corresponding class,
with Rabbit.prototype inheriting from Animal.prototype.
22 changes: 22 additions & 0 deletions jsinfo/extendCalc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function Calculator() {

let methods = {
"-": (a, b) => a - b,
"+": (a, b) => a + b,
};

this.calculate = function(str) {

let splitted = str.split(' '),
a = +splitted[0],
operator = splitted[1],
b = +splitted[2]

return methods[operator](a, b);
}


this.addMethod = function(name, func) {
methods[name] = func;
};
}
47 changes: 47 additions & 0 deletions jsinfo/extended_clock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Clock {
constructor({ template }) {
this._template = template;
}

_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);
}

stop() {
clearInterval(this._timer);
}

start() {
this._render();
this._timer = setInterval(() => this._render(), 1000);
}
}

class ExtendedClock extends Clock {
constructor(options) {
super(options);
let {precision = 1000} = options;
this._precision = precision;
}

start() {
this._render();
this._timer = setInterval(() => this._render(), this._precision);
}
};
15 changes: 15 additions & 0 deletions jsinfo/hamsters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let hamster = {
stomach: [],

eat(food) {
this.stomach = [food];
}
};

let speedy = {
_proto_: hamster
};

let lazy = {
_proto_: hamster
};
7 changes: 7 additions & 0 deletions jsinfo/newAccu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function Accumulator(startingValue) {
this.value = startingValue;

this.read = function() {
this.value += prompt("number!");
};
}
17 changes: 17 additions & 0 deletions jsinfo/newCalc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function Calculator() {
this.read() = function() {
this.a = +prompt("value 1?")
this.b = +prompt("value 2?")
},

this.sum() = function() {
return this.a + this.b;
},

this.mul() = function(){
return this.a * this.b;
};

}

let calculator = new Calculator();
32 changes: 32 additions & 0 deletions jsinfo/rewrite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Clock{

constructor({template}) {
this._template = template;
}

_render() {
let date = new Date();
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);
}

stop() {
clearInterval(this._timer);
}

start() {
this._render();
this._timer = setInterval(() => this._render(), 1000);
};
Loading