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 new file mode 100644 index 0000000..086ca1a --- /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..4a7e52d --- /dev/null +++ b/jsinfo/chaining.js @@ -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 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/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() ); 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)