From f9eaf5363eb65062fcf2cbc303b15df1df80aaaa Mon Sep 17 00:00:00 2001 From: Alex Reany Date: Tue, 25 Apr 2017 11:39:13 -0700 Subject: [PATCH] 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