diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..496ee2c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store \ No newline at end of file diff --git a/exercises/001.js b/exercises/001.js index 962fdaa..f0dff60 100644 --- a/exercises/001.js +++ b/exercises/001.js @@ -3,7 +3,7 @@ Write a function 'transformFirstAndLast' that takes in an array, and returns an 1) the first element of the array as the object's key, and 2) the last element of the array as that key's value. Example input: -['Queen', 'Elizabeth', 'Of Hearts', 'Beyonce'] +array = ['Queen', 'Elizabeth', 'Of Hearts', 'Beyonce'] Function's return value (output): { Queen : 'Beyonce' @@ -21,7 +21,12 @@ function transformFirstAndLast(array) { // your code here } */ +// const arr = ['Queen', 'Elizabeth', 'Of Hearts', 'Beyonce'] function transformFirstAndLast(array) { - //your code here -} \ No newline at end of file + let obj = {}; + obj[array[0]] = array[array.length - 1]; + return obj; +} +// console.log(transformFirstAndLast(arr)) +// console.log(arr); diff --git a/exercises/002.js b/exercises/002.js index 71af29a..84c3dc9 100644 --- a/exercises/002.js +++ b/exercises/002.js @@ -25,6 +25,14 @@ function getAllKeys(obj) { } */ +// const object = {key1: 'value1', key2: 'value2'}; + function getAllKeys(obj){ - // your code here + const arr = []; + for(let key in obj) { + arr.push(key.toString()); + } + return arr; } + +// console.log(getAllKeys(object)); \ No newline at end of file diff --git a/exercises/003.js b/exercises/003.js index 8f0a5d4..11c4994 100644 --- a/exercises/003.js +++ b/exercises/003.js @@ -19,7 +19,14 @@ For instance, if the input had 6 values instead of 4, your code should flexibly Starter Code: */ -function fromListToObject(array) { - // your code here +// const arr = [['make', 'Ford'], ['model', 'Mustang'], ['year', 1964]]; +function fromListToObject(array) { + const obj = {}; + array.forEach(element => { + obj[element[0]] = element[1]; + }) + return obj; } + +// console.log(fromListToObject(arr)); \ No newline at end of file diff --git a/exercises/004.js b/exercises/004.js index caed05d..b283550 100644 --- a/exercises/004.js +++ b/exercises/004.js @@ -27,7 +27,19 @@ Function's return value (output): Starter Code */ +// const object = { +// a : 'a', +// number : 11, +// hungry : true, +// grammyWins : 1 +// } + function listAllValues(obj) { - // your code here + const arr = []; + for(key in obj) { + arr.push(obj[key]); + } + return arr; +} -} \ No newline at end of file +// console.log(listAllValues(object)); \ No newline at end of file diff --git a/exercises/005.js b/exercises/005.js index eeccb70..e771bcf 100644 --- a/exercises/005.js +++ b/exercises/005.js @@ -24,7 +24,37 @@ For example, let's say the HR department adds a "tshirtSize" field to each emplo Starter Code : */ +// const input = [ +// [ +// ['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk'] +// ], +// [ +// ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager'] +// ], +// [ +// ['firstName', 'Extra'], ['lastName', 'Spicy'], ['age', 234], ['role', 'hard code spoiler'] +// ] +// ]; + function transformEmployeeData(array) { - // your code here + const output = []; + array.forEach(el => { + output.push(fromListToObject(el)); + }) + return output; +} + +// From previous exercise +function fromListToObject(array) { + const obj = {}; + array.forEach(element => { + obj[element[0]] = element[1]; + }) + return obj; } + +// const output = transformEmployeeData(input); +// console.log(output); + + diff --git a/exercises/006.js b/exercises/006.js index dca4c27..316b86e 100644 --- a/exercises/006.js +++ b/exercises/006.js @@ -21,6 +21,36 @@ E.g., it should also be able to handle this, or any other object containing simp Starter Code: */ +// const input = { +// name: 'Holly', +// age: 35, +// role: 'producer', +// extra: 'Uh Oh' +// } + function convertObjectToList(obj) { - // your code here -} \ No newline at end of file + const output = []; + for(let i = 0; i < getAllKeys(obj).length; i++){ + output[i] = [getAllKeys(obj)[i], listAllValues(obj)[i]]; + } + return output; +} +// From previous exercises + +function listAllValues(obj) { + const arr = []; + for(key in obj) { + arr.push(obj[key]); + } + return arr; +} + +function getAllKeys(obj){ + const arr = []; + for(let key in obj) { + arr.push(key.toString()); + } + return arr; +} + +// console.log(convertObjectToList(input)); \ No newline at end of file diff --git a/exercises/007.js b/exercises/007.js index 094e8b5..5fe243d 100644 --- a/exercises/007.js +++ b/exercises/007.js @@ -33,24 +33,26 @@ Starter Code : */ var customerData = { - 'Joe': { - visits: 1 - }, - 'Carol': { - visits: 2 - }, - 'Howard': { - visits: 3 - }, - 'Carrie': { - visits: 4 - } + 'Joe': {visits: 1}, + 'Carol': {visits: 2}, + 'Howard': {visits: 3}, + 'Carrie': {visits: 4} }; +// console.log(customerData['Joe'].visits); function greetCustomer(firstName) { - var greeting = ''; - // your code here - - return greeting; -} + let greeting = ''; + for(name in customerData){ + if(name === firstName){ + if(customerData[name].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!`; + } + return greeting; + } + } + return greeting = `Welcome! Is this your first time?`; +} +// console.log(greetCustomer(""));