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
17 changes: 16 additions & 1 deletion bike-shop/src/stage1-literals.js
Original file line number Diff line number Diff line change
@@ -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
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.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 = {
Expand Down
20 changes: 20 additions & 0 deletions jsinfo/An_error_in_the_inheritance.js
Original file line number Diff line number Diff line change
@@ -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())
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
}
};
19 changes: 19 additions & 0 deletions jsinfo/Create_a_calculator.js
Original file line number Diff line number Diff line change
@@ -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());
26 changes: 26 additions & 0 deletions jsinfo/Create_an_extendable_calculator.js
Original file line number Diff line number Diff line change
@@ -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);
12 changes: 12 additions & 0 deletions jsinfo/Create_new_accumulator.js
Original file line number Diff line number Diff line change
@@ -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);
20 changes: 20 additions & 0 deletions jsinfo/Create_new_calculator.js
Original file line number Diff line number Diff line change
@@ -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());
8 changes: 8 additions & 0 deletions jsinfo/Hello_object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let user = {}

user.name = 'John'
user.surname = 'Smith'

user.name = 'Pete'

delete user.name
15 changes: 15 additions & 0 deletions jsinfo/Multiply_numeric_properties_by_2.js
Original file line number Diff line number Diff line change
@@ -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)
34 changes: 34 additions & 0 deletions jsinfo/Rewrite_to_prototypes.js
Original file line number Diff line number Diff line change
@@ -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);
};
19 changes: 19 additions & 0 deletions jsinfo/Searching_algorithm.js
Original file line number Diff line number Diff line change
@@ -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
};
23 changes: 23 additions & 0 deletions jsinfo/Why_two_hamsters_are_full.js
Original file line number Diff line number Diff line change
@@ -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 );