Hello world,
I have a few questions figure out how my code passed.
-
Why didn’t I have to use a for in loop for this to work?
-
Why didn’t I have to use quotation syntax on
obj [name]
in the if statement? -
Why did I have to use
obj[name].visits
to get the value of visits? When there is an object in an object do I need to use dot notation even after calling the key pair value?
shouldn’t I simply be able to useobj[name]
Here is the challenge:
Given the customerData object below, write a function greetCustomer that takes in a similar object and a name. Given that name, return a greeting based on how many times that person has visited your establishment. Essentially, you will check the name given to your function against the names in the object given to your function.
const customerData = {
'Joe': {
visits: 1
},
'Carol': {
visits: 2
},
'Howard': {
visits: 3
},
'Carrie': {
visits: 4
}
}
function greetCustomer(customerData, name){
const obj = customerData;
let greeting = '';
if (!obj[name]){
greeting = 'Welcome! Is this your first time?';
return greeting;
}
else if (obj[name].visits === 1){
greeting = 'Welcome back,' + ' ' + name +'! We\'re glad you liked us the first time!';
return greeting;
}
if ( obj[name].visits > 1 ) {
greeting = 'Welcome back,' + ' ' + name + '! It\'s good to see a regular customer';
return greeting;
}
}
// If the customer is not present in customerData:
//greetCustomer(customerData, 'Terrance');
//console.log(greetCustomer(customerData, 'Terrance'))// 'Welcome! Is this your first time?'
// If the customer has visited only once ('visits' value is 1):
//greetCustomer(customerData, 'Joe'); // 'Welcome back, Joe! We're glad you liked us the first time!'
//console.log(greetCustomer(customerData, 'Joe'));
// If the customer is a repeat customer: ('visits' value is greater than 1)
greetCustomer(customerData, 'Carol'); // 'Welcome back, Carol! It's good to see a regular customer'
//console.log(greetCustomer(customerData, 'Carol'));te code here