Been stuck on this.
Why does this not get evaluated and executed?
if (position > 1){
welcome = welcomeText2
return welcome
function:
const customerData = {
'Joe': {
visits: 1
},
'Carol': {
visits: 2
},
'Howard': {
visits: 3
},
'Carrie': {
visits: 4
}
};
var myValues = Object.values(customerData);
var myObjects = Object.keys(customerData);
function isInside(array, target){
var found = false;
for (var i = 0; i < array.length; i++){
if (array[i] == target){
found = true;
}
}
return found
}
function greetCustomer(customerData, name){
var welcome = " ";
if (isInside(myObjects, name) === false){
welcome = `Welcome! Is this your first time?`;}
for (let i = 0; i < myObjects.length; i++){
var welcomeText1 = `Welcome back, ${name}! We're glad you liked us the first time!`;
var welcomeText2 = `Welcome back, ${name}! I'ts good to see a regular customer`;
if (isInside(myObjects, name) === true){
var position = 0
for(var j in customerData){
position++
if (name === j){
if (position == 1){
welcome = welcomeText1
return welcome
if (position > 1){
welcome = welcomeText2
return welcome
}
}
}
}
}
return welcome
}
}
greetCustomer(customerData, 'Howard');
That if statement is inside another if statement that has a return
.
if (position == 1) {
welcome = welcomeText1;
// returns out of loop
return welcome;
if (position > 1) {
welcome = welcomeText2;
return welcome;
}
}
Thank you, I should have seen that. I edited and still could not get that if statement to trigger.
const customerData = {
'Joe': {
visits: 1
},
'Carol': {
visits: 2
},
'Howard': {
visits: 3
},
'Carrie': {
visits: 4
}
};
var myValues = Object.values(customerData);
var myObjects = Object.keys(customerData);
function isInside(array, target){
var found = false;
for (var i = 0; i < array.length; i++){
if (array[i] == target){
found = true;
}
}
return found;
}
function greetCustomer(customerData, name){
var welcome = " ";
if (isInside(myObjects, name) === false){
welcome = `Welcome! Is this your first time?`;}
for (let i = 0; i < myObjects.length; i++){
var welcomeText1 = `Welcome back, ${name}! We're glad you liked us the first time!`;
var welcomeText2 = `Welcome back, ${name}! I'ts good to see a regular customer`;
if (isInside(myObjects, name) === true){
var position = 0;
for(var j in customerData){
position++;
if (name === j){
console.log(position);
console.log(position > 1);
if (position == 1){
welcome = welcomeText1;
if (position > 1){
console.log('hello');
welcome = welcomeText2;
return welcome;
}
}
}
}
}
return welcome;
}
}
greetCustomer(customerData, 'Howard');
Try removing all the returns from the if statements and just have the one return welcome
at the end as you have now.
Edit: You still have the position
if statements inside each other as well. Just some clarification if position
equals 1 it can’t be greater than 1 as well.