From 6a0385c2d4d6c844643f445ecb026e2d4a339531 Mon Sep 17 00:00:00 2001 From: Alex Reany Date: Mon, 24 Apr 2017 12:57:32 -0700 Subject: [PATCH 1/4] stage 1 complete --- index.html | 10 ++++++++ jsinfo/chaining.js | 46 ++++++++++++++++++++++++++++++++++ jsinfo/createACalc.js | 20 +++++++++++++++ jsinfo/helloObject.js | 15 +++++++++++ jsinfo/multiplyNumericByTwo.js | 23 +++++++++++++++++ 5 files changed, 114 insertions(+) create mode 100644 index.html create mode 100644 jsinfo/chaining.js create mode 100644 jsinfo/createACalc.js create mode 100644 jsinfo/helloObject.js create mode 100644 jsinfo/multiplyNumericByTwo.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..449f707 --- /dev/null +++ b/index.html @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/jsinfo/chaining.js b/jsinfo/chaining.js new file mode 100644 index 0000000..e7247ad --- /dev/null +++ b/jsinfo/chaining.js @@ -0,0 +1,46 @@ +// 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 diff --git a/jsinfo/createACalc.js b/jsinfo/createACalc.js new file mode 100644 index 0000000..ac12bf2 --- /dev/null +++ b/jsinfo/createACalc.js @@ -0,0 +1,20 @@ +// Create an object calculator with three methods: read , sum, mul + +let calculator = { + read() { + 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() ); diff --git a/jsinfo/helloObject.js b/jsinfo/helloObject.js new file mode 100644 index 0000000..4c037f7 --- /dev/null +++ b/jsinfo/helloObject.js @@ -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; diff --git a/jsinfo/multiplyNumericByTwo.js b/jsinfo/multiplyNumericByTwo.js new file mode 100644 index 0000000..fd901ce --- /dev/null +++ b/jsinfo/multiplyNumericByTwo.js @@ -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) From 0cb9ebe706582930cc13295ec750350152461021 Mon Sep 17 00:00:00 2001 From: Alex Reany Date: Mon, 24 Apr 2017 17:45:50 -0700 Subject: [PATCH 2/4] finished stage 1 --- bike-shop/src/stage1-literals.js | 20 ++++++++++++++++++-- index.html | 2 +- jsinfo/chaining.js | 1 - jsinfo/createNewAccumlator.js | 28 ++++++++++++++++++++++++++++ jsinfo/createNewCalc.js | 29 +++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 jsinfo/createNewAccumlator.js create mode 100644 jsinfo/createNewCalc.js diff --git a/bike-shop/src/stage1-literals.js b/bike-shop/src/stage1-literals.js index c749dd2..e18a7e2 100644 --- a/bike-shop/src/stage1-literals.js +++ b/bike-shop/src/stage1-literals.js @@ -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 diff --git a/index.html b/index.html index 449f707..086ca1a 100644 --- a/index.html +++ b/index.html @@ -5,6 +5,6 @@ - + diff --git a/jsinfo/chaining.js b/jsinfo/chaining.js index e7247ad..4a7e52d 100644 --- a/jsinfo/chaining.js +++ b/jsinfo/chaining.js @@ -35,7 +35,6 @@ let ladder = { down() { this.step--; return this; - }, // shows the current step showStep: function() { diff --git a/jsinfo/createNewAccumlator.js b/jsinfo/createNewAccumlator.js new file mode 100644 index 0000000..c7f06f8 --- /dev/null +++ b/jsinfo/createNewAccumlator.js @@ -0,0 +1,28 @@ +/*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) { + + this.read = function () { + this.value1 = parseFloat(prompt("what is the first value")); + startingValue += this.value1; + console.log(startingValue); + }; + + this.value = function (startingValue) { + return Accumulator.startingValue + } +} + + +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 diff --git a/jsinfo/createNewCalc.js b/jsinfo/createNewCalc.js new file mode 100644 index 0000000..836aa8c --- /dev/null +++ b/jsinfo/createNewCalc.js @@ -0,0 +1,29 @@ +/*Create a constructor function Calculator +that creates objects with 3 methods: +read() sum() mul() */ + +function Calculator() { + + this.read = function () { + 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() ); From f9eaf5363eb65062fcf2cbc303b15df1df80aaaa Mon Sep 17 00:00:00 2001 From: Alex Reany Date: Tue, 25 Apr 2017 11:39:13 -0700 Subject: [PATCH 3/4] finished stage 2 --- bike-shop/src/stage2-constructors.js | 43 ++++++++++++++++++++++++---- index.html | 2 +- jsinfo/createNewAccumlator.js | 11 +++---- jsinfo/extendableCalculator.js | 40 ++++++++++++++++++++++++++ jsinfo/searthingAlgo.js | 0 5 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 jsinfo/extendableCalculator.js create mode 100644 jsinfo/searthingAlgo.js diff --git a/bike-shop/src/stage2-constructors.js b/bike-shop/src/stage2-constructors.js index c422386..b52ede6 100644 --- a/bike-shop/src/stage2-constructors.js +++ b/bike-shop/src/stage2-constructors.js @@ -1,17 +1,48 @@ -function Bike() { - // your code here +function Bike(name, price) { + alert(new.target); + 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 = "" diff --git a/index.html b/index.html index 086ca1a..d873de8 100644 --- a/index.html +++ b/index.html @@ -5,6 +5,6 @@ - + diff --git a/jsinfo/createNewAccumlator.js b/jsinfo/createNewAccumlator.js index c7f06f8..dceab73 100644 --- a/jsinfo/createNewAccumlator.js +++ b/jsinfo/createNewAccumlator.js @@ -9,16 +9,13 @@ In other words, the value property is the sum of all user-entered values with th function Accumulator(startingValue) { + //holds the startingValue to be minipulated + this.value = startingValue; this.read = function () { - this.value1 = parseFloat(prompt("what is the first value")); - startingValue += this.value1; - console.log(startingValue); + //adds the prompted amount to the value at the moment + this.value += parseFloat(prompt("what is the first value")); }; - - this.value = function (startingValue) { - return Accumulator.startingValue - } } diff --git a/jsinfo/extendableCalculator.js b/jsinfo/extendableCalculator.js new file mode 100644 index 0000000..3c975a7 --- /dev/null +++ b/jsinfo/extendableCalculator.js @@ -0,0 +1,40 @@ +//Create a calculator that can take a string and return the value +function Calculator() { + + 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 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")); diff --git a/jsinfo/searthingAlgo.js b/jsinfo/searthingAlgo.js new file mode 100644 index 0000000..e69de29 From c4302ea19cce0d39c056f6c976d05bf8da93f3ed Mon Sep 17 00:00:00 2001 From: Alex Reany Date: Tue, 25 Apr 2017 11:42:33 -0700 Subject: [PATCH 4/4] finished stage 2 --- bike-shop/src/stage2-constructors.js | 8 -------- jsinfo/searthingAlgo.js | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/bike-shop/src/stage2-constructors.js b/bike-shop/src/stage2-constructors.js index b52ede6..73bf4b0 100644 --- a/bike-shop/src/stage2-constructors.js +++ b/bike-shop/src/stage2-constructors.js @@ -34,15 +34,7 @@ module.exports = { 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 = "" diff --git a/jsinfo/searthingAlgo.js b/jsinfo/searthingAlgo.js index e69de29..05d877f 100644 --- a/jsinfo/searthingAlgo.js +++ b/jsinfo/searthingAlgo.js @@ -0,0 +1,22 @@ +/* +Use __proto__ to assign prototypes in a way that any property +lookup will follow the path: pockets → bed → table → head. For instance, +pockets.pen should be 3 (found in table), and bed.glasses should be 1 (found in head). +*/ + +let head = { + glasses: 1 +}; + +let table = { + pen: 3 +}; + +let bed = { + sheet: 1, + pillow: 2 +}; + +let pockets = { + money: 2000 +};