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
20 changes: 18 additions & 2 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 : {
color : "blue",
height: 55,
style : "cruiser"
},
brakes : {
back : true,
front : false
},
tires : {
diameter : 22,
type : "fat"
},
rings : [2, 5]
};

module.exports = myBike
42 changes: 36 additions & 6 deletions bike-shop/src/stage2-constructors.js
Original file line number Diff line number Diff line change
@@ -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 = ""
54 changes: 52 additions & 2 deletions bike-shop/src/stage3-methods.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,65 @@
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
}


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
59 changes: 57 additions & 2 deletions bike-shop/src/stage4-inheritance.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,69 @@ class Frame {
}

class Tire {
// your code here

}

class Bike {
// your code here
constructor() {
this.tires = [new Tire, new Tire]
this.frame = new Frame
this.brakes = {
back : true,
front : true
}
this.rings = [3, 7]
}
}

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

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

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



//let mountainBike = new MountainBike()
//let racingBike = new RacingBike()



module.exports = {
Bike: Bike,
MountainBike: MountainBike,
BMXBike: BMXBike,
RacingBike: RacingBike
// you'll need to export new classes here
}
10 changes: 10 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script src="bike-shop/src/stage4-inheritance.js"></script>
</body>
</html>
45 changes: 45 additions & 0 deletions jsinfo/chaining.js
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions jsinfo/createACalc.js
Original file line number Diff line number Diff line change
@@ -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() );
25 changes: 25 additions & 0 deletions jsinfo/createNewAccumlator.js
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions jsinfo/createNewCalc.js
Original file line number Diff line number Diff line change
@@ -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() );
26 changes: 26 additions & 0 deletions jsinfo/errorCreatingAnInstance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Here’s the code with Rabbit extending Animal.

Unfortunately, Rabbit objects can’t be created.
What’s wrong? Fix it.
*/
class Animal {

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

}

class Rabbit extends Animal {
constructor(name) {
//need to add a supper function to allow inheritance
super(name)
//now with the super()
//this.name = name;
this.created = Date.now();
}
}

let rabbit = new Rabbit("White Rabbit"); // Error: this is not defined
alert(rabbit.name);
Loading