diff --git a/exercises.js b/exercises.js index 2ee51b9..ed581a2 100644 --- a/exercises.js +++ b/exercises.js @@ -13,8 +13,16 @@ Objects in Javascript can be compared to objects in real life. Objects has a col Console.log the object. */ - +var books = { + title: 'The Obstacle is the Way', + author: 'Ryan Holiday', + category: 'Self Help', + pages: 200, +} + +console.log(books); + /* 2. Declare a variable named `dog` and create the following properties (key-value pairs) to the object: @@ -30,7 +38,17 @@ Objects in Javascript can be compared to objects in real life. Objects has a col "My dog `name` is `age` year old and likes to `speak` at strangers." */ +var dog = { + name: 'o3q8weytoq8wetynolsaiufdloqweit', + age: 92357982475274508723095098423, + vegeterian: false, + color: ['black', 'white'], + speak: function(){ + return 'Bark' + } +} +console.log('My dog ' + dog.name + ' is ' + dog.age + ' years old and likes to ' + dog.speak() + ' at strangers.') //An empty object @@ -47,6 +65,17 @@ Objects in Javascript can be compared to objects in real life. Objects has a col Console.log the object. */ +var kicks = {}; + +kicks.brand = 'nike'; +kicks.color = 'cornflower blue'; +kicks.size = 7.5; +kicks.price = 75; +kicks.buy = function(){ + return 'Now I is a cool kid!'; +} + +console.log(kicks); /* @@ -59,6 +88,13 @@ Objects in Javascript can be compared to objects in real life. Objects has a col console.log the object */ +var plainBox = {}; + +plainBox.color = 'blue'; +plainBox.size = 12; +plainBox.contents = []; + +console.log(plainBox); /* 5. Declare a variable named `stockCar` and create the following properties (key-value pairs) to the object: @@ -71,6 +107,15 @@ Objects in Javascript can be compared to objects in real life. Objects has a col console.log the object */ +var stockCar = { + model: 'ford', + year: 2014, + automaticTransmission: true, + driver: null, + passengers: [] +} + +console.log(stockCar); /* @@ -89,6 +134,17 @@ Objects in Javascript can be compared to objects in real life. Objects has a col the value at `name`, and just the value at `age`. */ +var plainPerson = {}; + +function buildPerson(person, nameString, age){ + plainPerson.name = nameString; + plainPerson.age = age; + return person; +} + +var completePerson = buildPerson('boy', 'James', 12); +console.log(completePerson); +console.log(plainPerson); /* 7. Display values of objects that are inside an array @@ -115,6 +171,121 @@ Objects in Javascript can be compared to objects in real life. Objects has a col ... */ +var arrayOfObjects = [ + + { + + id: 0, + + date: "Monday Jan 25 2015 2:01 PM", + + total: "279.38" + + }, + + { + + id: 1, + + date: "Monday Jan 27 2015 11:31 AM", + + total: "79.80" + + }, + + { + + id: 2, + + date: "Monday Feb 1 2015 7:56 AM", + + total: "15.62" + + }, + + { + + id: 3, + + date: "Monday Feb 1 2015 9:43 AM", + + total: "19.83" + + }, + + { + + id: 4, + + date: "Monday Feb 1 2015 11:08 PM", + + total: "56.69" + + }, + + { + + id: 5, + + date: "Monday Feb 13 2015 10:22 AM", + + total: "137.92" + + }, + + { + + id: 6, + + date: "Monday Feb 14 2015 6:54 PM", + + total: "938.65" + + }, + + { + + id: 7, + + date: "Monday Feb 14 2015 7:17 PM", + + total: "43.77" + + }, + + { + + id: 8, + + date: "Monday Feb 14 2015 7:18 PM", + + total: "28.54" + + }, + + { + + id: 9, + + date: "Monday Feb 14 2015 7:18 PM", + + total: "194.33" + + } + + ] + +function printOrders(orders){ + for(var i = 0; i < orders.length; i++){ + console.log('====='); + console.log('id: ' + orders[i].id); + console.log('date: ' + orders[i].date); + console.log('total: ' + orders[i].total); + } + +} + +printOrders(arrayOfObjects); /* 8. Addition with an object @@ -129,6 +300,19 @@ Objects in Javascript can be compared to objects in real life. Objects has a col to inspect your results. */ +var sumObj = { + a: 3, + b: 4, + result: undefined, +} + +function objectAddition(obj){ + var sum = obj.a + obj.b; + result = sum; + console.log(result); +} + +objectAddition(sumObj); /* 9. Print sum function and add as new key-value @@ -147,6 +331,12 @@ Objects in Javascript can be compared to objects in real life. Objects has a col **create more** objects and invoke your function multiple times. */ +function printObj(obj){ + var sum = obj.a + obj.b; + console.log(obj.a + ' + ' + obj.b + ' = ' + sum); +} + +printObj(sumObj); /* 10. Putting stuff in `plainBox` @@ -158,6 +348,16 @@ Objects in Javascript can be compared to objects in real life. Objects has a col plainBoxResult and use `console.log` to inspect your results. */ +function putInPlainBox(object){ + for (var i = 0; i < 10; i++){ + var randomNum = Math.floor(Math.random()*10); + object.contents.push(randomNum); + } + return object; +} + +var plainBoxResult = putInPlainBox(plainBox); +console.log(plainBoxResult); /* 11. Detecting transmission @@ -170,6 +370,15 @@ Objects in Javascript can be compared to objects in real life. Objects has a col Invoke your function and pass in your stockCar object, store the result to a variable named isAutomaticTransmission and use `console.log` to inspect your results. */ +function detectingTransmission(object){ + if (object.automaticTransmission === true){ + console.log(true); + }else{ + console.log(false); + } +} + +detectingTransmission(stockCar); /* 12. Who's driving this thing?! @@ -183,7 +392,14 @@ Objects in Javascript can be compared to objects in real life. Objects has a col your results. Consider using `plainPerson` as your driver. */ +function addDriver(obj, person){ + obj.driver = person; + return obj; +} + +var stockCarWithDriver = addDriver(stockCar, plainPerson); +console.log(stockCarWithDriver); /*