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 = ""
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
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/stage3-methods.js" charset="UTF-8"></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() );
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");
42 changes: 42 additions & 0 deletions jsinfo/extendableCalculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//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)
},
//arrow function
"-" : (value1, value2) => parseFloat(value1) - parseFloat(value2),
}

this.calculate = function (str) {

let strSplit = str.split(" ");
var value1 = strSplit[0];
var value2 = strSplit[2];
var operator = strSplit[1];

//return the value of the string to be operated on
return methods[operator](value1, value2)
};

this.addOperator =function (name, func){
methods[name] = func;
}
}

let calc = new Calculator;

alert( calc.calculate("3 + 7") ); // 10

let newCalc = new Calculator

newCalc.addOperator("*", (a, b) => a * b);
newCalc.addOperator("/", (a, b) => a / b);
newCalc.addOperator("**", (a, b) => a ** b)
newCalc.addOperator("pie", (a,b) => "I have " + a + " pies cut into " + b + " slices each")
alert(newCalc.calculate("2 * 3")); //6
alert(newCalc.calculate("2 ** 3")); //8
alert(newCalc.calculate("6 / 2")); //3
alert(newCalc.calculate("7 pie 4"));
15 changes: 15 additions & 0 deletions jsinfo/helloObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//Hello, object

//Creating an object



let user = {
name : "John",
surname : "Smith"
};
// reassign name to "Pete"

user.name = "Pete";
// Delete the property of name
delete user.name;
23 changes: 23 additions & 0 deletions jsinfo/multiplyNumericByTwo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Create a function multiplyNumeric(obj) that multiplies
all numeric properties of obj by 2.*/

function multiplyNumeric(obj) {
//gets values of the keys in the object
for (let key in obj) {
/*makes sure that the type of is number
so you can actually multiply by two*/
if (typeof obj[key] === 'number') {
obj[key] *= 2;
}
}
}

//example object
let menu = {
width: 200,
height: 300,
title: "My menu"
};

//call the function
multiplyNumeric(menu)
Loading