0

EDIT: ADDED OBJECT

I'm having an issue with a variable declared within the body of a function that seems to disappear before the variable is returned from the function:

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

function greetCustomer(firstName) {
  var greeting = '';

  for(var key in customerData){
    if(key === firstName){
      if(customerData[key]['visits'] === 1){
         greeting = "Welcome back, " + firstName + "! We're glad you liked us the first time!";
        console.log(greeting); // here to illustrate issue
      }
      else if(customerData[key]['visits'] > 1){
        greeting = "Welcome back, " + firstName + "! So glad to see you again!";
        console.log(greeting);
      }
    }
    else{
      greeting = "Welcome! Is this your first time?"
    }
  }

  return greeting;
}
greetCustomer("Joe");

And the output:

Welcome back, Joe! We're glad you liked us the first time! // here is the correct greeting from the console output
=> 'Welcome! Is this your first time?' // this is what I got
Welcome back, Carol! So glad to see you again! // correct output again
=> 'Welcome! Is this your first time? // base case again.

Shouldn't greeting be visible throughout the function for accessing its value and for assignment as well? I know that I can just return the greeting from the branch, but I'm unsure as to what I'm seeing here, but I hope someone can explain. Thanks.

7
  • 2
    Your output is not clear! Please explain more! What do you expect? Commented Feb 22, 2017 at 21:46
  • 1
    You're looping through the data and every iteration writes something to the greeting variable. Only the value set in the last iteration is returned. Commented Feb 22, 2017 at 21:48
  • 1
    The variable is absolutely visible throughout your function. Your error is somewhere else Commented Feb 22, 2017 at 21:49
  • 1
    Try some debugging. Open your console and add a breakpoint at the start of the greetCustomer function. Does your customerData have a key Joe? Step through your for loop and compare the input Joe to the object. Commented Feb 22, 2017 at 21:49
  • what does customerData look like and where is it defined? the issue is most likely stemming from scoping issues with that instead Commented Feb 22, 2017 at 21:49

2 Answers 2

1

For success conditions immediately return greeting instead of assigning it to greeting variable. But for the condition where firstname is not one of keys in customerData, just set greeting to "Welcome! Is this your first time?" & let the iteration continue to look for `firstname.

Change your code to this [TESTED]:

function greetCustomer(firstName) {
var greeting = '';

for(var key in customerData){
  if(key === firstName){
    if(customerData[key]['visits'] === 1){
      return("Welcome back, " + firstName + "! We're glad you liked us the first time!");
      console.log(greeting); // here to illustrate issue
    }
    else if(customerData[key]['visits'] > 1){
      return("Welcome back, " + firstName + "! So glad to see you again!");
     }
   }
   else{
    greeting = "Welcome! Is this your first time?";
  }
}
return greeting;
}
console.log(greetCustomer("Joe"));
Sign up to request clarification or add additional context in comments.

Comments

1

Your code iterates over the available keys in the customerData object. When it sees a key matching firstName it assigns to greeting and logs it.

Then it continues to iterate over the other keys and if the next key does not match firstName (which it probably won't), greeting is assigned "Welcome! Is this your first time?". This value is eventually returned from the function. When firstName happens to be the last key in customerData then your code will work OK, otherwise greeting will be reassigned with the default value.

So the basic problem is that your code continues processing after it has found and processed the target value, and that it reassigns the greetings string. There is no issue with scope or visibility of the greeting variable - it is a local variable to function greetCustomer.

One way to fix this is to return immediately after assigning the greeting (as you already noted). Another way is to use break to exit the for loop. Then greeting will be returned at the end of the function.

function greetCustomer(firstName) {
  var greeting = '';

  for(var key in customerData){
    if(key === firstName){
      if(customerData[key]['visits'] === 1){
         greeting = "Welcome back, " + firstName + "! We're glad you liked us the first time!";
        console.log(greeting); // here to illustrate issue
      }
      else if(customerData[key]['visits'] > 1){
        greeting = "Welcome back, " + firstName + "! So glad to see you again!";
        console.log(greeting);
      }
      break;    // customer name found and appropriate greeting set, exit loop
    }
    else{
      greeting = "Welcome! Is this your first time?"
    }
  }

  return greeting;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.