Search code examples
javascriptfunctional-programming

I need to find how many friends of a given people array have names that start with a given letter


So I'm working on a problem that I've been staring at for far too long. I have an array of people, each represented by an object. Inside each person object is a value for friends, which is an array.

The friends array contains an object for each friend, with one of the key/value pairs being name: 'name'.

I need to find how many friends of a given person have names that start with given letter.

Below is the array of people, including the friends array for each person. After that is the code I've worked with out so far, trying to use reduce.

var customers = [{
    name: "Courtney",
    age: 43,
    balance: "$3,400",
    friends: [{
        id: 0,
        name: "Justice Lara"
    }, {
        id: 1,
        name: "Duke Patrick"
    }, {
        id: 2,
        name: "Herring Hull"
    }, {
        id: 3,
        name: "Johnnie Berg"
    }]
}, {
    name: "Regina",
    age: 53,
    balance: "$4,000",
        friends: [{
        id: 0,
        name: "Cheryl Kent"
    }, {
        id: 1,
        name: "Greta Wells"
    }, {
        id: 2,
        name: "Gutierrez Waters"
    }, {
        id: 3,
        name: "Cooley Jimenez"
    }]
}, {
    name: "Jay",
    age: 28,
    balance: "$3,000",
    friends: [{
        id: 0,
        name: "Cross Barnett"
    }, {
        id: 1,
        name: "Raquel Haney"
    }, {
        id: 2,
        name: "Cassandra Martin"
    }, {
        id: 3,
        name: "Shelly Walton"
    }]
}];

var friendFirstLetterCount = function(array, customer, letter) {
    let friendNames = [];
    for (let i = 0; i < customer.friends.length; i++) {
        friendNames.push(customer.friends[i].name);
    }

    let friendCount = _.filter(friendNames, function(current, index, array) {
    return current.toLowerCase().charAt(0) === letter.toLowerCase(); 
  });

    return friendCount.length;
};

Solution

  • This seems to work. Read the inline comments for an explanation of how:

    function friendFirstLetterCount(array, customer, letter) {
      
      // Find the customer object in the array, where the customer name is equal to customer.
      const customerObj = array.filter(arrayCustomer => arrayCustomer.name === customer)[0];
      
      // Return zero if customer not found.
      // (Up to you how you handle this, you might want an exception instead).
      if (!customerObj) return 0;
      
      // Find all customer friends whose name starts with letter and return the length.
      return customerObj.friends.filter(friend => friend.name.startsWith(letter)).length;
    }
    

    Test run

    friendFirstLetterCount(customers, "Regina", "C");
    

    Result

    2

    There are likely a few edge cases you want to watch out for; for example, what happens if the customer isn't found (see comments), and what happens if the customer doesn't have any friends.

    Edit: I also think it's more readable with the following parameter and variable names

    function friendFirstLetterCount(array, name, letter) {
      
      // Find the customer object in the array, where the customer name is equal to customer.
      const customer = array.filter(customer => customer.name === name)[0];
      
      // Return zero if customer not found.
      if (!customer) return 0;
      
      // Find all customer friends whose name starts with letter and return the length.
      return customer.friends.filter(friend => friend.name.startsWith(letter)).length;
    }