diff --git a/exercises/001.js b/exercises/001.js index 962fdaa..d035249 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 obj = {}; + obj[array[0]] = array[array.length - 1]; + return obj; +} diff --git a/exercises/002.js b/exercises/002.js index 71af29a..e762e67 100644 --- a/exercises/002.js +++ b/exercises/002.js @@ -25,6 +25,12 @@ function getAllKeys(obj) { } */ -function getAllKeys(obj){ - // your code here +function getAllKeys(obj) { + var ret = []; + + for (key in obj) { + ret.push(key); + } + + return ret; } diff --git a/exercises/003.js b/exercises/003.js index 8f0a5d4..e11caab 100644 --- a/exercises/003.js +++ b/exercises/003.js @@ -13,13 +13,19 @@ 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 ret = {}; + for(i = 0; i < array.length; i++) + { + ret[array[i][0]] = array[i][1]; + } + + return ret; } diff --git a/exercises/004.js b/exercises/004.js index caed05d..f168db5 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,12 @@ Starter Code */ function listAllValues(obj) { - // your code here + var ret = []; -} \ No newline at end of file + for(key in obj) + { + ret.push(obj[key]); + } + + return ret; +} diff --git a/exercises/005.js b/exercises/005.js index eeccb70..bc87569 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,19 @@ Starter Code : */ function transformEmployeeData(array) { - // your code here + var ret = []; + for(i = 0; i < array.length; i++) + { + var newObj = {}; + + for(j = 0; j < array[i].length; j++) + { + newObj[array[i][j][0]] = array[i][j][1]; + } + + ret.push(newObj); + } + + return ret; } diff --git a/exercises/006.js b/exercises/006.js index dca4c27..04ca9c0 100644 --- a/exercises/006.js +++ b/exercises/006.js @@ -22,5 +22,12 @@ Starter Code: */ function convertObjectToList(obj) { - // your code here -} \ No newline at end of file + var ret = []; + + for(key in obj) + { + ret.push([key, obj[key]]); + } + + return ret; +} diff --git a/exercises/007.js b/exercises/007.js index 094e8b5..924b0ec 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,16 @@ var customerData = { }; function greetCustomer(firstName) { - var greeting = ''; - // your code here + var greeting = 'Welcome! Is this your first time?'; + + if (firstName in customerData) { + if (customerData[firstName].visits == 1) { + greeting = "Welcome back, " + firstName + "! We're glad you liked us the first time!"; + } + else if (customerData[firstName].visits > 1) { + greeting = "Welcome back, " + firstName + "! So glad to see you again!"; + } + } return greeting; } -