S Cloud
S Cloud

Reputation: 41

Javascript - For Loop not finding values in array correctly

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?

Upvotes: 0

Views: 110

Answers (6)

user2314737
user2314737

Reputation: 29307

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');

Upvotes: 0

Rani Das
Rani Das

Reputation: 19

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');
    }
}

Upvotes: 0

abigperson
abigperson

Reputation: 5362

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');
}

Upvotes: 1

xale94
xale94

Reputation: 424

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

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386540

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');
    }
}

Upvotes: 1

Mark Adelsberger
Mark Adelsberger

Reputation: 45659

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...

Upvotes: 2

Related Questions