0

Some Background Information

There was a lesson on Codecademy that instructed me to create a contact list (features objects within objects). At the end, I was to make a function that could take an input and match it with any names on the contact list. If there was a match, it would log all of the contact information associated with that name to the console.

So far, I have successfully done all of the above and wanted to go a little further. I wanted the function to log the address of the person with spacing in between each value (like this: "One Microsoft Way, Redmond, WA, 98052" instead of "One Microsoft Way,Redmond,WA,98052").

I also wanted the function to log "No match." into the console (just once) if it searched through the entire contact list and found no matching names. With my current code, it logs "No match." for every single non-matching name, and I don't want that.

In short: How can I return and separate an array of values with spacing between each value? How can I make the function return "No match." a single time after not finding any matches?

Here is my code:

// Main object
var friends = {
    // Nested objects
    bill: {
        firstName: "Bill",
        lastName: "Pifheba",
        number: "1234567890",
        address: ["One Microsoft Way", "Redmond", "WA", "98052"]
    },

    steve: {
        firstName: "Steve",
        lastName: "Ajfiae",
        number: "1234512345",
        address: ["2 Stack Way", "Lazss", "WA", "12345"]
    },

    bob: {
        firstName: "Bob",
        lastName: "Dcfiwae",
        number: "8291047261",
        address: ["28 Stack Way", "What", "WA", "54321"]
    }
};

// Searches for matching name
var search = function(name) {
    // Iterates through nested objects
    for (var x in friends) {
        // If match is found
        if (friends[x].firstName === name) {
            // Print contact information
            console.log("First Name: " + friends[x].firstName + "\r\nLast Name: " + friends[x].lastName + "\r\nNumber: " + friends[x].number + "\r\nAddress: " + friends[x].address);
        } else {
            console.log("No match.");
        }
    }
};

// Asks for who to search for
search(prompt("Name?"));

4 Answers 4

1

You can .join an array to turn it into a space separated string.

["One Microsoft Way", "Redmond", "WA", "98052"].join(", ");
// "One Microsoft Way, Redmond, WA, 98052"

Rather than logging the value inside your function, you can return it and instead log the result of calling it.

if (friends[x].firstName === name) {
  return "First Name: " + friends[x].firstName + "\r\nLast Name: " + friends[x].lastName + "\r\nNumber: " + friends[x].number + "\r\nAddress: " + friends[x].address);
}

This will short circuit the loop as soon as it finds a match. To provide a fallback return value, simply add another return statement at the end of the function.

var search = function(name) {
  // ...
  return "No match.";
}

This statement will only run if the loop completes without finding a match.

Finally you can directly log the result of the call.

console.log(search(prompt("Name?")));
Sign up to request clarification or add additional context in comments.

Comments

0
var search = function(name) {
    // Iterates through nested objects
    var found = false;
    for (var x in friends) {
        // If match is found
        if (friends[x].firstName === name) {
            // Print contact information
            found = true;
            console.log("First Name: " + friends[x].firstName + "\r\nLast Name: " + friends[x].lastName + "\r\nNumber: " + friends[x].number + "\r\nAddress: " + friends[x].address.join(' '));
            break; // stop iteration
        }
    }
    if (!found) {
      console.log("No match.");
    }
};

Comments

0

You can use Array.prototype.join(), which joins the array elements together with the string supplied to the method (here ', '), to form a string:

friends[x].address.join(', ')

References:

Comments

0

There is a couple of issues with your code.

  • you should use an array so you can have multiple users with the same name
  • you don't really need to loop through all your friends as each key is unique

see comments in code below

// Main object
var friends = {
  // Nested objects
  bill: {
    firstName: "Bill",
    lastName: "Pifheba",
    number: "1234567890",
    address: ["One Microsoft Way", "Redmond", "WA", "98052"]
  },

  steve: {
    firstName: "Steve",
    lastName: "Ajfiae",
    number: "1234512345",
    address: ["2 Crap Way", "Lazss", "WA", "12345"]
  },

  bob: {
    firstName: "Bob",
    lastName: "Dcfiwae",
    number: "8291047261",
    address: ["28 Crap Way", "What", "WA", "54321"]
  }
};

// Searches for matching name
var search = function(name) {
  // check to see if the friends name is a property of your object
  // if not return early
  if ( name in friends == false ) {
    console.log('not found');
    return false;
  }
  // since you have already checked that the name exists you 
  // can just use the name property instead of looping through
  // as all keys must be unique when using an object
  console.log( friends[name] );
  console.log("First Name: " + friends[name].firstName + "\r\nLast Name: " + friends[name].lastName + "\r\nNumber: " + friends[name].number + "\r\nAddress: " + friends[name].address.join(' '));
      // you can join an array with `.join`
      console.log(friends[name].address.join(' '));
};

// Asks for who to search for
search(prompt("Name?"));
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>

1 Comment

I know that, but on Codecademy, you are required to use an object with nested objects and at least 1 for/in loop in your code. It's probably because I'm in the OOP section.

Your Answer

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.