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
21 changes: 20 additions & 1 deletion bike-shop/src/stage1-literals.js
Original file line number Diff line number Diff line change
@@ -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
24 changes: 18 additions & 6 deletions bike-shop/src/stage2-constructors.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
function Bike() {
// your code here

function Frame(color = 'black', size = 55, style = 'street') {
this.color = color,
this.size = size,
this.style = style
}

function Frame() {
// your code here
function Tire(diameter = 22, type = 'street') {
this.diameter = diameter,
this.type = type
}

function Tire() {
// your code here
function Bike(name = 'Speedster', price = 250.00) {
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
}
}

module.exports = {
Expand Down
48 changes: 43 additions & 5 deletions bike-shop/src/stage3-methods.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,55 @@
function Bike() {
// your code here
this.moving = false,
this.tires = [new Tire(), new Tire()],
this.rings = [3, 7]

}

function Frame() {
// your code here
Bike.prototype.isMoving = function() {
return this.moving
}

function Tire() {
// your code here
Bike.prototype.pedal = function() {
if (this.tires[0]._flat === true || this.tires[1]._flat === true ) {
throw new Error("Can't pedal with a flat tire")
} else {

this.moving = true
}
}

Bike.prototype.brake = function() {
return this.moving = false
}

Bike.prototype.gearSpeeds = function() {
return this.rings.reduce((acc, next) => {
return acc * next
}, 1)
};

function Tire() {
this._flat = false
}

Tire.prototype.isFlat = function() {
return this._flat
}

Tire.prototype.puncture = function() {
this._flat = true
}

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




module.exports = {
Bike: Bike,
Frame: Frame,
//Frame: Frame,
Tire: Tire
}
20 changes: 20 additions & 0 deletions jsinfo/Create_calculator.js
Original file line number Diff line number Diff line change
@@ -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() );
27 changes: 27 additions & 0 deletions jsinfo/Extendable_Calculator.js
Original file line number Diff line number Diff line change
@@ -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;
};
}
8 changes: 8 additions & 0 deletions jsinfo/Hello_Object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//Hello Object

let user = {}
user.name: 'John';
user.surname: 'Smith';
user.name = 'Pete';

delete user.name
22 changes: 22 additions & 0 deletions jsinfo/Inheritance_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//An error in inheritance

function Animal(name) {
this.name = name;
}

Animal.prototype.walk = function() {
alert(this.name + ' walks');
};

function Rabbit(name) {
this.name = name;
}

//--> Rabbit.prototype = Animal.prototype <--
//error here

Rabbit.protoype.__proto__ = Animal.prototype; //<-- fix

Rabbit.prototype.walk = function() {
alert(this.name + " bounces!");
};
17 changes: 17 additions & 0 deletions jsinfo/Ladder.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
26 changes: 26 additions & 0 deletions jsinfo/Multipy_numeric_properties.js
Original file line number Diff line number Diff line change
@@ -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"
};
16 changes: 16 additions & 0 deletions jsinfo/New_Accumulator.js
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 23 additions & 0 deletions jsinfo/New_Calculator.js
Original file line number Diff line number Diff line change
@@ -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() );
36 changes: 36 additions & 0 deletions jsinfo/Prototype_Rewrite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//Rewrite to Prototype

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(() => ._render(), 1000);
};

}
21 changes: 21 additions & 0 deletions jsinfo/Searching_Algorithm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//Searching algorithm

let head = {
glasses: 1
};

let table = {
pen: 3,
__proto__: head
};

let bed = {
sheet: 1,
pillow: 2,
__proto__: table
};

let pockets = {
money: 2000,
__proto__: bed
};
24 changes: 24 additions & 0 deletions jsinfo/Why_two_hamsters_full.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//Why Are two hamsters full?

let hamster = {
stomach: [],

eat(food) {
this.stomach = [food]; //<-- fix
}
};

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