diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..73821be Binary files /dev/null and b/.DS_Store differ diff --git a/README.md b/README.md index 54b2dfb..c5fe9e1 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ This is the base repository for the [Init 2: OOP Practice with Bike Shop](http:/ 1. Install Node.js and npm
Follow the instructions in this [Treehouse blog](http://blog.teamtreehouse.com/install-node-js-npm-mac) to install [Node.js](https://nodejs.org/en/) and [npm](https://www.npmjs.com/) using Homebrew on your Mac. 1. Do your work in the subdirectories (`bike-shop/`, `jsinfo/`, `music-player/`) -
Be sure to read the `README.md` files in the subdiretories as well. +
Be sure to read the `README.md` files in the subdirectories as well. ## Resources diff --git a/bike-shop/src/stage1-literals.js b/bike-shop/src/stage1-literals.js index c749dd2..01881b1 100644 --- a/bike-shop/src/stage1-literals.js +++ b/bike-shop/src/stage1-literals.js @@ -1,5 +1,26 @@ 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 diff --git a/bike-shop/src/stage2-constructors.js b/bike-shop/src/stage2-constructors.js index c422386..6a68d8c 100644 --- a/bike-shop/src/stage2-constructors.js +++ b/bike-shop/src/stage2-constructors.js @@ -1,13 +1,30 @@ -function Bike() { - // your code here +function Bike(name, price) { + this.name = name; + this.price = price; + this.rings = [3,7]; + this.brakes = { + front: true, + back: true } + this.frame = new Frame(); + this.tires = []; + this.tires[0] = new Tire(); + this.tires[1] = new Tire(); -function Frame() { - // your code here } -function Tire() { - // your code here + +function Frame(color, size, style) { + this.color = color || "black"; + this.size = size || 55; + this.style = style || "street"; + + } + + +function Tire(diameter, type) { + this.diameter = diameter || 22, + this.type = type || "street" } module.exports = { diff --git a/bike-shop/src/stage3-methods.js b/bike-shop/src/stage3-methods.js index c422386..58cb824 100644 --- a/bike-shop/src/stage3-methods.js +++ b/bike-shop/src/stage3-methods.js @@ -1,17 +1,53 @@ function Bike() { - // your code here -} + this.isMoving = function(){ + return false; +}; + this.tires = [new Tire(), new Tire()] + this.pedal = function (){ + if (this.tires[0].isFlat() || this.tires[1].isFlat()) { + throw "Can't pedal with a flat tire" + } + + this.isMoving = function (){ + return true; + } + } + this.brake = function(){ + this.isMoving = function(){ + return false; + } + } + this.gearSpeeds = function(){ + } + this.rings = [3,7]; + this.gearSpeeds = function(){ + return this.rings[0] *this.rings[1] + } + } + function Frame() { // your code here } function Tire() { - // your code here + this.isFlat = function(){ + return false; + } + this.puncture = function(){ + this.isFlat = function(){ + return true; + } + }; + this.repair = function(){ + this.isFlat = function(){ + return false; + }; + } } module.exports = { Bike: Bike, Frame: Frame, Tire: Tire -} +}; diff --git a/bike-shop/src/stage4-inheritance.js b/bike-shop/src/stage4-inheritance.js index 2281fd5..9dfc78c 100644 --- a/bike-shop/src/stage4-inheritance.js +++ b/bike-shop/src/stage4-inheritance.js @@ -1,16 +1,58 @@ class Frame { -// your code here + constructor () { + this.style = "default"; + } } class Tire { - // your code here + constructor (){ + this.type = "default"; + this.diameter = 20; + + } +} + +class Bike { + constructor (){ + this.frame = new Frame; + this.tires = [new Tire(), new Tire()]; + this.brakes = new Brakes; + } +} + +class MountainBike extends Bike { + constructor () { super(); + this.frame.style = "mountain"; + this.tires[0].type = "dirt"; + this.tires[1].type = "dirt"; + this.shocks = 20; + } + + adjustShocks (num){ + this.shocks = num; + } +} + +class BMXBike extends Bike { +} + +class RacingBike extends Bike { + // this.style = "racing" + // this.type = "road" + } -class Bike { - // your code here +class Brakes { + constructor(){ + this.front = false; + this.back = true; + } } module.exports = { Bike: Bike, + MountainBike: MountainBike, + BMXBike: BMXBike, + RacingBike: RacingBike, // you'll need to export new classes here } diff --git a/bike-shop/test/stage3.test.js b/bike-shop/test/stage3.test.js index 4f82562..34b0673 100644 --- a/bike-shop/test/stage3.test.js +++ b/bike-shop/test/stage3.test.js @@ -95,6 +95,7 @@ describe('Bike', () => { test('can tell the number of speeds with #gearSpeeds()', () => { const myBike = new Bike() + myBike.rings = [3,7] expect(myBike.gearSpeeds()).toBe(21) myBike.rings = [2,5] expect(myBike.gearSpeeds()).toBe(10) diff --git a/jsinfo/Accumulator.js b/jsinfo/Accumulator.js new file mode 100644 index 0000000..025530e --- /dev/null +++ b/jsinfo/Accumulator.js @@ -0,0 +1,11 @@ +var Accumulator = function(startingValue){ + this.value = startingValue; + + this.read = function (){ + this.value += +prompt("enter a number:", 0); + }; +} + + + +let accumulator = new Accumulator() diff --git a/jsinfo/calculator.js b/jsinfo/calculator.js new file mode 100644 index 0000000..287f647 --- /dev/null +++ b/jsinfo/calculator.js @@ -0,0 +1,12 @@ +let calculator = { + sum () { + return this.a + this.b; + }, + mul () { + return this.a * this.b; + }, + read () { + this.a = +prompt('first number', 0); + this.b = +prompt('second number', 0); + } +} diff --git a/jsinfo/chaining.js b/jsinfo/chaining.js new file mode 100644 index 0000000..3f5f00e --- /dev/null +++ b/jsinfo/chaining.js @@ -0,0 +1,17 @@ +let ladder = { + step: 0, + up() { + this.step++; + return this; + }, + + down() { + this.step--; + return this; + }, + + showStep: function(){ //current step + alert( this.step); + return this; + } + } diff --git a/jsinfo/extendableCalculator.js b/jsinfo/extendableCalculator.js new file mode 100644 index 0000000..bb6f940 --- /dev/null +++ b/jsinfo/extendableCalculator.js @@ -0,0 +1,33 @@ +//method calculate(str) takes a str and returns result +//make "+" = + +//make "-" = - +//add operator constructor function +//func will = calculate(str)^^ + +function calculate(){ + this.calculate = function(str){ + + var split = str.split(" ") + var a = parseInt(split[0]); //num + var b = parseInt(split[2]); //num + let operator = split[1] // op + } + +} +//method obj + +//each operator stores in an array + + + }; +this.addMethod = function(name, func) +// function addOperator(name, func){ +// this.name = name; +// this.func = func; //** +// +// } + //calculator.add methods of operations = function created, actual function) + this.addMethod = function(name, func) { + methods.name = func;// what does this mean? + }; +// diff --git a/jsinfo/helloObject.js b/jsinfo/helloObject.js new file mode 100644 index 0000000..bc1a78b --- /dev/null +++ b/jsinfo/helloObject.js @@ -0,0 +1,9 @@ +let user = {} +user.name = "John" +user.surname = "Smith" +user.name = "Pete" +//Object.assign (user, {name: "Pete", isAdmin: true}) +delete user.name +//Object.assign (user, {name: "Pete", isAdmin: true}) + +console.log(user) diff --git a/jsinfo/multNumericBy2.js b/jsinfo/multNumericBy2.js new file mode 100644 index 0000000..0cc3168 --- /dev/null +++ b/jsinfo/multNumericBy2.js @@ -0,0 +1,17 @@ +let fee = { adult: 5, child: 10, title: "Entrance Fee"} + + + + function multiplyNumeric(obj) { + for (let key in obj){ + if (typeof obj[key] == 'number'){ + obj[key] *= 2 + } + } + } + //if value === num + //mult value x + + +console.log(multiplyNumeric(fee)) +//github diff --git a/jsinfo/newCalculator.js b/jsinfo/newCalculator.js new file mode 100644 index 0000000..fdfc309 --- /dev/null +++ b/jsinfo/newCalculator.js @@ -0,0 +1,23 @@ +//calculator is a constructor function w 3 methods + +//this.read is a function/prompts for values store as obj properties +//sum a + b +//mul a * b + +function calculator(){ //calculator org func + this.read = function (){ + this.a = +prompt("enter first number:", 0) //num 1 + this.b = +prompt("enter second number:", 0) //num 2 + }, + this.sum = function (){ //calculator sum obj/func/prop + return this.a + this.b; //return num a + b + }, + this.mul = function (){ + return this.a * this.b; + } + +} +let calculator = new Calculator; + +alert( "Sum =" + calculator.sum() ); +alert( "Mul =" + calculator.mul() ); diff --git a/jsinfo/searching.js b/jsinfo/searching.js new file mode 100644 index 0000000..49b9af7 --- /dev/null +++ b/jsinfo/searching.js @@ -0,0 +1,21 @@ + + +let head = { + glasses: 1 +}; + +let table = { + pen: 3 + _proto_: head +}; + +let bed = { + sheet: 1, + pillow: 2 + _proto_: table +}; + +let pockets = { + money: 2000 + _proto_: bed +}; diff --git a/music-player/album.js b/music-player/album.js index 877a3aa..991eef1 100644 --- a/music-player/album.js +++ b/music-player/album.js @@ -1 +1,11 @@ -// Your code here + +const Artist = require('/.artist'); +class Album { + constructor (title, songcount, artist){ + this.title = title; + this.songcount = songcount; + this.artist = artist; + } +} + +module.exports = Album diff --git a/music-player/artist.js b/music-player/artist.js index 877a3aa..c792b5e 100644 --- a/music-player/artist.js +++ b/music-player/artist.js @@ -1 +1,11 @@ -// Your code here +class Artist { + constructor(name, bandmembers, genre){ + this.name = name; + this.bandmembers = bandmembers; + this.genre = genre; + } +} + +let littleDragon = new Artist ("Little Dragon", ["Yukimi Nagano", "Erik Bodin", "Hakan Wirenstrand", "Frederik Wallin"], "Indie"); + +console.log(littleDragon.artist) diff --git a/music-player/song.js b/music-player/song.js index 877a3aa..5bb0e1b 100644 --- a/music-player/song.js +++ b/music-player/song.js @@ -1 +1,30 @@ -// Your code here +class Song { + constructor(title){ + this.name = name; + this.length = length; + } + + Song.prototype = { + play: function(){ + return this.name + "plays"; + }, + pause: function(){ + return this.name + "pauses"; + }; + } + +} + +// } +// +// class Artist { +// constructor(name, bandmembers, genre){ +// this.name = name; +// this.bandmembers = bandmembers; +// this.genre = genre; +// } +// } +// +// let littleDragon = new Artist (("Little Dragon", "Yukimi Nagano, Erik Bodin, Hakan Wirenstrand, Frederik Wallin", "Indie")); +// +// diff --git a/npm-debug.log b/npm-debug.log new file mode 100644 index 0000000..c2062c4 --- /dev/null +++ b/npm-debug.log @@ -0,0 +1,23 @@ +0 info it worked if it ends with ok +1 verbose cli [ '/usr/local/bin/node', +1 verbose cli '/usr/local/bin/npm', +1 verbose cli 'run', +1 verbose cli 'test:stage2' ] +2 info using npm@3.8.6 +3 info using node@v6.0.0 +4 verbose stack Error: ENOENT: no such file or directory, open '/Users/Jaszly/LG/oop-practice/package.json' +4 verbose stack at Error (native) +5 verbose cwd /Users/Jaszly/LG/oop-practice +6 error Darwin 15.4.0 +7 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "test:stage2" +8 error node v6.0.0 +9 error npm v3.8.6 +10 error path /Users/Jaszly/LG/oop-practice/package.json +11 error code ENOENT +12 error errno -2 +13 error syscall open +14 error enoent ENOENT: no such file or directory, open '/Users/Jaszly/LG/oop-practice/package.json' +15 error enoent ENOENT: no such file or directory, open '/Users/Jaszly/LG/oop-practice/package.json' +15 error enoent This is most likely not a problem with npm itself +15 error enoent and is related to npm not being able to find a file. +16 verbose exit [ -2, true ]