From 18f72430eb52604451516cb785ed82d3fddec780 Mon Sep 17 00:00:00 2001 From: Wes Jourdan Date: Sat, 13 Jan 2018 19:37:21 -0500 Subject: [PATCH] All tests passed. --- exercises/001.js | 6 ++++-- exercises/002.js | 10 +++++++++- exercises/003.js | 9 ++++++--- exercises/004.js | 12 ++++++++---- exercises/005.js | 22 +++++++++++++++++++--- exercises/006.js | 18 ++++++++++++++++-- exercises/007.js | 21 ++++++++++++++++----- this/this.js | 25 +++++++++++++++++++++++++ 8 files changed, 103 insertions(+), 20 deletions(-) create mode 100644 this/this.js diff --git a/exercises/001.js b/exercises/001.js index 962fdaa..26984f8 100644 --- a/exercises/001.js +++ b/exercises/001.js @@ -23,5 +23,7 @@ function transformFirstAndLast(array) { */ function transformFirstAndLast(array) { - //your code here -} \ No newline at end of file + var newObj = {}; + newObj[array[0]] = array[array.length - 1]; + return newObj; +} diff --git a/exercises/002.js b/exercises/002.js index 71af29a..871f20a 100644 --- a/exercises/002.js +++ b/exercises/002.js @@ -26,5 +26,13 @@ function getAllKeys(obj) { */ function getAllKeys(obj){ - // your code here + var keys = []; + var x; + for (x in obj) { + keys.push(x); + } + return keys; } + +getAllKeys({name: "wes", age: "34", location: "NC"}) +console.log(getAllKeys({name: "wes", age: "34", location: "NC"})); diff --git a/exercises/003.js b/exercises/003.js index 8f0a5d4..f383ad9 100644 --- a/exercises/003.js +++ b/exercises/003.js @@ -13,13 +13,16 @@ Function's return value (output): Do not change the input string. Assume that all elements in the array will be of type 'string'. -Note that the input may have a different number of elements than the given sample. +Note that the input may have a different number of elements than the given sample. For instance, if the input had 6 values instead of 4, your code should flexibly accommodate that. Starter Code: */ function fromListToObject(array) { - // your code here - + var listObj = {}; + for (var i = 0; i < array.length; i++) { + listObj[array[i][0]] = array[i][1]; + } + return listObj; } diff --git a/exercises/004.js b/exercises/004.js index caed05d..7d801cf 100644 --- a/exercises/004.js +++ b/exercises/004.js @@ -11,7 +11,7 @@ Example input: Function's return value (output): ['Krysten', 33, false] -Note that the input may have a different number of keys and values than the given sample. +Note that the input may have a different number of keys and values than the given sample. E.g. it should also handle an input like: { @@ -28,6 +28,10 @@ Starter Code */ function listAllValues(obj) { - // your code here - -} \ No newline at end of file + var keys = Object.keys(obj); + var valArray = [] + for (var i = 0; i < keys.length; i++){ + valArray.push(obj[keys[i]]); + }; + return valArray; +}; diff --git a/exercises/005.js b/exercises/005.js index eeccb70..074ce47 100644 --- a/exercises/005.js +++ b/exercises/005.js @@ -17,7 +17,7 @@ Given that input, the return value should look like this: {firstName: 'Mary', lastName: 'Jenkins', age: 36, role: 'manager'} ] -Note that the input may have a different number of rows or different keys than the given sample. +Note that the input may have a different number of rows or different keys than the given sample. For example, let's say the HR department adds a "tshirtSize" field to each employee record. Your code should flexibly accommodate that. @@ -25,6 +25,22 @@ Starter Code : */ function transformEmployeeData(array) { - // your code here - + var empObj = {}; + var empArray = []; + for (var i = 0; i < array.length; i++) { + for (var j = 0; j < array[i].length; j++) { + empObj[array[i][j][0]] = array[i][j][1]; + } + empArray.push(JSON.parse(JSON.stringify(empObj))); + } +return empArray; } + +transformEmployeeData([ + [ + ['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk'] + ], + [ + ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager'] + ] +]); diff --git a/exercises/006.js b/exercises/006.js index dca4c27..6a0d075 100644 --- a/exercises/006.js +++ b/exercises/006.js @@ -22,5 +22,19 @@ Starter Code: */ function convertObjectToList(obj) { - // your code here -} \ No newline at end of file + var keyArray = Object.keys(obj); + var converted = []; + var tempArray = []; + for (var i = 0; i < keyArray.length; i++) { + tempArray.push(keyArray[i]); + tempArray.push(obj[keyArray[i]]); + converted.push(tempArray); + tempArray = []; + } + return converted; +} +convertObjectToList({ + name: 'Holly', + age: 35, + role: 'producer' +}); diff --git a/exercises/007.js b/exercises/007.js index 094e8b5..34cba32 100644 --- a/exercises/007.js +++ b/exercises/007.js @@ -1,11 +1,11 @@ /* Write a function called "greetCustomer". -Given a name, "greetCustomer" returns a greeting based on how many times that customer has visited the restaurant. Please refer to the customerData object. +Given a name, "greetCustomer" returns a greeting based on how many times that customer has visited the restaurant. Please refer to the customerData object. The greeting should be different, depending on the name on their reservation. -Case 1 - Unknown customer ( Name is not present in customerData ): +Case 1 - Unknown customer ( Name is not present in customerData ): var output = greetCustomer('Terrance'); console.log(output); // --> 'Welcome! Is this your first time?' @@ -48,9 +48,20 @@ var customerData = { }; function greetCustomer(firstName) { - var greeting = ''; - // your code here +if (customerData[firstName] == undefined) { + return "Welcome! Is this your first time?"; +}; + + var greeting; + var visits = customerData[firstName]["visits"]; + if (visits == 1) { + greeting = "Welcome back, " + firstName + "! We're glad you liked us the first time!" + } else { + greeting = "Welcome back, " + firstName + "! So glad to see you again!" + }; + // your code here + console.log(greeting); return greeting; } - +greetCustomer("Joe"); diff --git a/this/this.js b/this/this.js new file mode 100644 index 0000000..46a01a1 --- /dev/null +++ b/this/this.js @@ -0,0 +1,25 @@ +var coffeeShop = { + beans: 40, + drinkRequirements: { + latte: 10, + americano: 5, + doubleShot: 15, + frenchPress: 12 + }, + makeDrink: function (drinkType) { + if (this.drinkRequirements[drinkType] && this.beans >= this.drinkRequirements[drinkType]) { + this.beans -= this.drinkRequirements[drinkType]; + console.log(drinkType + " is ready!"); + } else if (!this.drinkRequirements[drinkType]) { + console.log("Sorry, we don't make " + drinkType); + } else if (this.beans < this.drinkRequirements[drinkType]) { + console.log("Sorry, we're all out of beans!"); + } + } +}; + +coffeeShop.makeDrink('latte'); +coffeeShop.makeDrink('americano'); +coffeeShop.makeDrink('pourOver'); +coffeeShop.makeDrink('doubleShot'); +coffeeShop.makeDrink('frenchPress');