Skip to content

Instantly share code, notes, and snippets.

@revisualize
Last active August 8, 2017 02:50
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save revisualize/4c38d3548ff935ee28e945c81dfe652d to your computer and use it in GitHub Desktop.

Gist for the FreeCodeCamp Profile Lookup Challenge. Instructions as comments. Total: 39 lines.

We have an array of objects representing different people in our contacts lists.

Example: var contacts = [ { ... } , { ... } , { ... } , { ... } ];

// A lookUpProfile function that takes
// firstName and a property (prop)
// as parameters has been pre-written for you.
function lookUpProfile(firstName, prop){
   // The function should check if firstName is an actual contact's firstName
   // and the given property (prop) is a property key of that contact.

   // If both are true, then return the "value" of that property.

   // If prop does not correspond to any valid property keys
   // then return "No such property"


   // If firstName does not correspond to any contacts then return "No such contact"
   
}

Hints: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

There is a key word in the if firstName does not corrospond to ANY contacts in the contact list.

And you need to look at the array of objects. And ask yourself how you're going to access data

from the objects inside the array elements.

And you need to look at the fact that the last six lesson were all about loops.

A few key previous lessons for solving this challenge:

Accessing Nested Objects

Accessing Nested Arrays

Accessing Objects Properties with the Dot Operator

Accessing Objects Properties with Bracket Notation

Accessing Objects Properties with Variables

Testing Objects for Properties

I'd also like to restate what was learned in "Return Early Pattern for Functions":

When a return statement is reached, the execution of the current function stops and control returns to the calling location.

If you're trying to run a loop and return, it will halt the function in turn halting the loop.

The last hint that I'd like to give is to think that you have a large stack of business cards, how would you psuedo-code accessing or returning information from the business cards?

How/when would you state that you don't have a business card for a person?

@revisualize
Copy link
Author

revisualize commented Dec 20, 2016

Here are some questions that I ask people over and over as they are working through this challenge:

Let's say you have an object.

// var name = "Happy";
var myFriend = {
        "firstName": "Happy",
        "lastName": "Feet",
        "number": "-i",
        "likes": ["rhythm", "dancing", "soul"]
    }

How do you access the firstName of myFriend?

Let's say we have an array:
var contacts = [ "A" , "H" , "S" , "K" ];
How do you access "H"?

Let's say we have an array:
var contacts = [ { f: "A" } , { f: "H" } , { f: "S" } , { f: "K" } ];
How do you output "H"?

What if f: was firstName: ?

.

Let's say you were at a big business event and throughout the event you got handed 200 business cards. Now, I walk up to you at the end and ask you.. Do you have a Business Card for "Jane Smith"? Now you loop look through your stack of 200 business cards. When would you tell me that you didn't have a business card for Jane?
My next question is going to be.. How does that question relate to the lesson that you're working on?

.

I also find that i have to point out a lot that:
Dot Notation is converting the value to a string (string literal). Example: myObj.name; is the same as myObj["name"]; and as we all know quotes define strings. If you want to use a variable for accessing the value of object properties you cannot use Dot Notation. You have to use Bracket Notation. Example: var num = 42; myObj[num];

.

I often have to remind people about:
Parameters are variables that represent the values that get passed into your function from the function call.
Image
Notice how the variables level and score in the function definition addScore are called parameters.
However, when we invoke the function like in:
addScore(3, 10) or addScore(6, 20)
the values are called arguments. Here is an important lesson:
You define a function with parameters, you call a function with arguments.

Another example of this:

function hello(fName, uName) {
     return "Hello " + fName + " " + uName + ", How is your day?";
}
hello("Joseph", "@revisualize"); // "Hello Joseph @revisualize, How is your day?"
hello("Bella", "@bellaknoti"); // "Hello Bella @bellaknoti, How is your day?"
hello("Andy", "@dirn"); // "Hello Andy @dirn, How is your day?"

You can use the fName and uName parameters just like a variable inside of your function.

Yet another example code:

function addThree (num) {
    var result;
    result = num + 3;
    return result;
}

You can see how the parameter is used like a variable inside of the function.
And you can do mathematical operations to the parameter and assign the value to the variable result.

Other important things to remember:
* A function can have zero parameters. You still have to use the parentheses to define it.
* A function might have no return statements. In this case we say that the function returns undefined.

From knowing that please note that technically, calling parameters variables isn't correct. Parameters are part of the function declaration and when the function is called, an execution context is formed and there parameters are variables that hold the passed arguments.

If the function's formal parameters do not include any default value initializers then the body declarations are instantiated in the same Environment Record as the parameters. If default value parameter initializers exist, a second Environment Record is created for the body declarations. Formal parameters and functions are initialized as part of FunctionDeclarationInstantiation. All other bindings are initialized during evaluation of the function body.
** — https://www.ecma-international.org/ecma-262/7.0/index.html#sec-functiondeclarationinstantiation**

.

And that...
return statements cause functions to halt and return. If you're trying to do a return inside of a loop it will cause the function to halt and return the information that you specify.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment