Hi there, I’m applying for Code Platoon and I’ve been stuck on one exercise for about a week! I’m really frustrated because to me, my code should be working.
I have a nested object. I need to first access the first keys of the object then the key of the keys object key. The object is as follows:
const customerData = {
'Joe': { visits: 1 },
'Carol': { visits: 2 },
'Howard': { visits: 3 },
'Carrie': {visits: 4 }
};```
As you can see, the key is a customerName and each customerName has an object with a key of visits and a value of each customer visit.
Accessing the customerName is very simple. I am using a for in loop like this:
for (var cust in customerData) { … } ```
This works just fine, but then I need to apply some conditions on BOTH the customerName (if it exists or not) and on the number of visits. Evaluating whether the customerName exists is also easy and has not problems. using:
if (customerData[cust] !== custName ) { ... }
else if (customerData[cust] === custName) { ... }
Accessing the number of visits also works with no problems when the user exists and the number of visits value is already set in the customerData Object by using:
num = customerData[custName].visits;
The problem is that when the customerName doesn’t exist, which means visits also does not exist, the program breaks. Of course, I’m getting a type error because visits is undefined since it does not exist.
I’ve tried a ton of ways to get around this. Some of which were calling num at the end of the first condition where the custName evaluates to false because it does not exist. This seems to work and presents the correct greeting based on my conditions, but I’m still not passing the exercise. It keeps telling me it expects BOTH a welcome it’s your first time greeting and the welcome back greeting immediately following that greeting. So what did I do to solve this problem? I created a label name and continued to beginning of the for loop to make the first condition true after adding the user when the first condition completes.
The problem: This does display what I was told was expected, but now I get an error saying the welcome back and regular customer greetings (condition 2 and 3) instead of what it originally wanted (condition 1 and 2). I still do not pass.
I have NO IDEA what I’m doing wrong here. Can someone please help me? When I remove the label, I pass the first part but not the 2nd. With the label, I fail both parts.
Thanks in advance!!!
Here’s the entire code:
// Write your greetCustomer function here
const customerData = {
'Joe': { visits: 1 },
'Carol': { visits: 2 },
'Howard': { visits: 3 },
'Carrie': {visits: 4 }
};
function greetCustomer (arr, custName) {
var greeting = "";
var num = 0;
loop:
for (var cust in customerData) {
if (customerData.hasOwnProperty(custName) === false) {
console.log(custName, "condition 1");
greeting = "Welcome! Is this your first time?";
customerData[custName] = {visits: 1};
continue loop;
}
if (customerData.hasOwnProperty(custName) === true) {
num = customerData[custName].visits;
console.log(num);
if (num === 1) {
console.log(custName, num, "condition 2");
greeting = "Welcome back, " + custName + "! We\'re glad you liked us the first time!";
break;
}
else {
console.log(custName, num, "condition 3");
greeting = "Welcome back, " + custName + "! It\'s good to see a regular customer.";
}
}
}
return greeting;
}
greetCustomer(customerData, "Emily");