简体   繁体   中英

How do I assign the value in a key value to the object key that contains it?

Directions: Write a function called "greetCustomers".

Given a name, "greetCustomers" 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 ):

var output = greetCustomer('Terrance'); console.log(output); // --> 'Welcome! Is this your first time?'

Case 2 - Customer who has visited only once ( 'visits' value is 1 ):

var output = greetCustomer('Joe'); console.log(output); // --> 'Welcome back, Joe! We're glad you liked us the first time!'

Case 3 - Repeat customer: ( 'visits' value is greater than 1 ):

var output = greetCustomer('Carol'); console.log(output); // --> 'Welcome back, Joe! So glad to see you again!'

Notes: * Your function should not alter the customerData object to update the number of visits.

 var customerData = {
 'Joe': {
    visits: 1
    },
'Carol': {
    visits: 2
    },
 'Howard': {
    visits: 3,
    },
'Carrie': {
    visits: 4
    }
  };

Here is my code:

 function greetCustomer(firstName){
var greeting = ''

   for (var keys in customerData){  
       for (var k in customerData[keys])

    if (customerData[keys][k] < 0 ) greeting = 'Welcome! Is this your first time?'
    else if (customerData[keys][k] === 1) greeting = "Welcome back, " + firstName + "! We're glad you liked us the first time!"
    else if (customerData[keys][k] >= 2) greeting = "Welcome back, " + firstName + "! So glad to see you again!"
      }

   return greeting
   }
greetCustomer("Carol")

Why doesn't this pass case one. Is there a better way to access the name and assign or relate the number of visits to it?

You don't need to iterate through the entire object, just lookup by their first name:

 var customerData = { 'Joe': { visits: 1 }, 'Carol': { visits: 2 }, 'Howard': { visits: 3, }, 'Carrie': { visits: 4 } }; function greetCustomer(firstName) { var greeting = ''; if (!customerData[firstName]) { // if they're not found in data, it's their first time greeting = 'Welcome! Is this your first time?'; } else if (customerData[firstName].visits === 1) { greeting = "Welcome back, " + firstName + "! We're glad you liked us the first time!"; } else if (customerData[firstName].visits >= 2) { greeting = "Welcome back, " + firstName + "! So glad to see you again!"; } return greeting; } // tests console.log(greetCustomer("Joe")); console.log(greetCustomer("Carol")); console.log(greetCustomer("Howard")); console.log(greetCustomer("Carrie")); console.log(greetCustomer("Bob")); 

Note : the last else if may be just plain else statement

var customerData = {
  'Joe': {
    visits: 1
  },
  'Carol': {
    visits: 2
  },
  'Howard': {
    visits: 3
  },
  'Carrie': {
    visits: 4
  }
};



function greetCustomer(firstName) {
 var greeting = '';
 if (!customerData[firstName]){
        greeting = 'Welcome! Is this your first time?';
 }else if (customerData[firstName].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;
}

console.log(greetCustomer('Carrie'));
//Welcome back, Carrie! So glad to see you again!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM