0

I have an array with a list of customers. I am declaring a function that takes a customer name as a parameter. I want this function to loop through the array to find out whether or not the customer is in the array.

 var customers = [
 {fullName: 'Marshall Hathers',
 dob: '01/07/1970'},
 {fullName: 'Margaret May',
 dob: '01/07/1980'}
 ];

The function:

 function findCustomer(customer) {
 for(i=0; i<customers.length; i++) {
 var found; 
 if(customer === customers[i].fullName) {
 found = true; 
 console.log('Customer has been found');
 break;
 } else {
 found = false;
 console.log('Customer has not been found');
 break;
 }
 }

It works well the first time a customer is found, but it prints out incorrectly when trying to find the second customer.

Can anyone please assist?

asked Jan 31, 2017 at 13:17
2
  • 1
    You should not break the for-loop if the first customer is not the one that you are looking for. Commented Jan 31, 2017 at 13:21
  • 3
    I'm voting to close this question as off-topic because it lacks minimal understanding. You should read a basic tutorial first. Commented Jan 31, 2017 at 13:21

6 Answers 6

2

So look at what you're actually saying in your loop. The loop body will run for each customer. So you're saying

For the first customer in the array
 if this is my customer
 print "found" and stop looping
 otherwise
 print "not found" and stop looping

Does that look right to you? Does looking at the first record alone really tell you that the customer isn't found?

And note that since all possibilities end with "and stop looping" the 2nd record is never examined. The whole point of a loop is that in at least some condition, you don't stop looping, right? So that you would then see the step repeated for the 2nd, and so on...

answered Jan 31, 2017 at 13:21
Sign up to request clarification or add additional context in comments.

Comments

1

Omit the else part and break the for loop if found.

function findCustomer(customer) {
 var found, i;
 for (i = 0; i < customers.length; i++) {
 if (customer === customers[i].fullName) {
 found = true;
 console.log('Customer has been found');
 break;
 }
 }
 if (!found) {
 console.log('Customer has not been found');
 }
}
answered Jan 31, 2017 at 13:23

Comments

1

Use the Array.some prototype function to find the element

function findCustomer(customer) {
 var found = customers.some(function(item) {return item.fullName == customer;});
 console.log(found ? 'Customer has been found': 'Customer has not been found');
}
answered Jan 31, 2017 at 13:25

Comments

0

You are exiting the loop when your script reach to a break

So if you look for the second customer, you will enter in the "else". And there you have got a break that exit from the loop, so you will never get the console.log

answered Jan 31, 2017 at 13:24

Comments

0

Just remove the break; statement from the else block; Here I rewritten the function for you.

function findCustomer(customer) {
var found = false; 
 for(i=0; i<customers.length; i++) {
 if(customer === customers[i].fullName) {
 found = true; 
 break;
 } else {
 found = false;
 }
 }
 if(found){
 console.log('Customer has been found');
 }else{
 console.log('Customer has not been found');
 }
}
answered Jan 31, 2017 at 13:40

Comments

0

I would change the code like this (edited as suggested in comment)

var customers = [{
 fullName: 'Marshall Hathers',
 dob: '01/07/1970'
}, {
 fullName: 'Margaret May',
 dob: '01/07/1980'
}];
function findCustomer(customer) {
 for (i = 0; i < customers.length; i++) {
 if (customer === customers[i].fullName) {
 console.log('Customer has been found');
 return true;
 }
 }
 console.log('Customer has not been found');
 return false;
}
findCustomer('Marshall Haters');

answered Jan 31, 2017 at 13:29

1 Comment

instead of setting a flag (found) and breaking the loop, you could simply exit the function with a return statement after you've found something. It's simpler.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.