-
-
Save revisualize/0de15e4716bf20d50dcac70e570de9a2 to your computer and use it in GitHub Desktop.
Side Question: | |
```js | |
function getName() { return "Happy Feet"; } | |
var name = getName(); | |
console.log(name); | |
``` | |
What do **you** think is output to the console when you log the variable `name`? | |
Why do you think that is? ... How does that relate to your question and to the challenge that you're working on? | |
Strings are Immutable. You have to completely reassign the string or create a new string. | |
Your function has declared and initialized the variable `result` to `""`. You are not modifying `result` and your function is returning `result`. Ergo, you're returning `""`. You need to modify `result` to be the value of the property from the object that you created. | |
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];` | |
[Mozilla Developer Network - Working with objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects) | |
```js | |
var num = 5; | |
num = num + 1; // 6 | |
num += 1; // 7 | |
num++; // 8 | |
``` | |
See how the one for getting `num` from `7` to `8` doesn't use the assignment operator (`=`)? | |
The lesson is requesting that you recreate that process so that you know how to increment and decrement a number with short notation. | |
**Increment (`++`)** The increment operator increments (adds one to) its operand and returns a value. | |
If used postfix, with operator after operand (for example, `x++`), then it returns the value before incrementing. | |
If used prefix with operator before operand (for example, `++x`), then it returns the value after incrementing. | |
Side note: The lesson is trying to teach you this now because this process is useful later when you need to loop through things to quickly increment (`++`) or the reverse decrement (`--`) from a value. | |
If you have a variable. `num` you can declare and initialize that with one line of code: | |
```js | |
var num = 10; | |
``` | |
Now. because you have a variable `num` which has been declared and instantiated with a value: 10 | |
You can do thins with that. Like add 5. | |
Now there are a few ways to add 5 to a variable | |
```js | |
num = num + 5; | |
``` | |
Which is kind of long but, it works. | |
OR! you can shorthand that to ... | |
```js | |
num += 5; | |
``` | |
At the end of using `+=` it takes the current value of `num` and adds and reassigns the value in one nice neat little line of code. | |
Counting Cards is a challenge where your function will receive a `card` parameter. You're supposed to increment or decrement the global `count` variable according to the `card`'s value | |
If the value of the `card` parameter is 2, 3, 4, 5 or 6 you need to increment the global variable `count`. | |
If the value of the `card` parameter is 10, 'J', 'Q', 'K' or 'A' you need to decrement the global variable `count`. | |
... | |
**Then** the function will return a string with the current `count` and the string "Bet" **if** the `count` is positive, or "Hold" **if** the count is zero or negative. | |
The current `count` and the player's decision ("Bet" or "Hold") should be separated by a single space. | |
... | |
... | |
Things to note: You should **NOT** be returning based on the value of the `card`. The value of `card` should only be adding or subtracting 1 from the global variable `count`. | |
The value of the global variable `count` is used to determine the return statement. | |
How to write an `if` conditional statements with OR and AND operators: | |
I am seeing a large number of people writing `if` conditions using OR and AND operators incorrectly. | |
Here is an example of what I'm seeing: | |
```js | |
if (param1 == "a" || "c" || "e") { // incorrect | |
var output = "Odd lowercase alpha character"; | |
} | |
``` | |
What I think people think is that it will compare `param1` to `"a"` then compare `param1` to `"c"` then compare `param1` to `"e"` | |
However, that's not actually what the computer is processing. | |
`if` conditions need to be evaluated to true for the `if` statement to run: | |
```js | |
if ((condition1) || (condition2) || (condition3) { | |
// true statement | |
} | |
``` | |
And how we would code that up from our example above where we're trying to get the odd character from a parameter (`param1`): | |
```js | |
if (param1 == "a" || param1 == "c" || param1 == "e") { | |
var output = "Odd lowercase alpha character"; | |
} | |
``` | |
I'm also seeing people writing and statements like this: | |
```js | |
if (param1 == "a" && param1 == "e") { | |
} | |
``` | |
The problem with this is the fact that a variable can't really be two strings or two different numbers at the same time. | |
The function declaration was set up as... `function phoneticLookup(val)` giving the function name as `phoneticLookup` and with the parameter of `val`. There is more that I could go into (if you want I can) but, I'll sum it up with: Parameters are variable-like objects that represent the values that get passed into your function from the function call (arguments). (You basically treat them like variables.) | |
So, as we learned in: [Accessing Objects Properties with Variables](https://www.freecodecamp.com/challenges/accessing-objects-properties-with-variables) you have to use bracket notation to access an object property with a variable. Example: `var num = 42;` `myObj[num];` | |
How do you modify the variable `result`? (Simple answer) | |
How do you access an object property value? | |
What if you need to access the object property value with a variable? | |
You need to re-assign the variable `result` the the object property value of the object that you created `lookup`. (As per the instructions) | |
With this code: `function phoneticLookup(val) { }` you have a function declaration with a function name of `phoneticLookup` and a parameter `val`. | |
When you make the function call: `phoneticLookup("charlie");` You are calling the function by name and passing the argument of `"charlie"` to the function parameter of `val` as a value. | |
You'll need to use bracket notation to access your object (`lookup`) property value. Note: Parameters are used inside of your function just like variables. | |
You need to assign the value of the object property to the variable `result`. | |
Then when your function `phoneticLookup` returns `result` it will return the correct object property value. | |
So, when you do ` lookup[val];` you're accessing the `lookup` with the parameter `val` that has a value of `"charlie"` and to get the object property value of `"Chicago"`. | |
With `result = lookup[val];` You're assigning that object property value to the variable `result`. Then you're returning `result`. | |
How do you assign the value of 7 to a variable `num`? ... `var num = 7;` | |
Now if you want you can use a variable to store values. | |
Like in mathmatics. `num` + 3 = 10 What is the value of `num`? | |
`var x = num + 3;` ... That outputs 10 | |
So, you can do several things with variables. But, you need to understand that variables hold values and variables can be used with other variables. | |
So, how do you assign the value of 5 to a variable `x`? ... `x = 5;` (Which is also overwriting the previous value of 10 from above. | |
How do you assign the value of `x` to a variable `y`? | |
Look at the way things are written. `var y = x;` | |
How do you assign the value of 42 to a variable `answer`? ... What is the answer? | |
Variables hold values. Just understand that. | |
Shortly put, parameters are part of the function declaration. | |
When a function call is made, JS engine makes an execution context where the function code is run. | |
There parameters become variables that hold the passed arguments. | |
They are treated just like variables on the execution context variable environment. Execution context has a global object that holds all these parameters and variables. | |
Parameters are like variables that represent the values that get passed into your function from the function call. | |
https://cs.wellesley.edu/~cs110/lectures/L16/images/function-anatomy.png | |
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: | |
``` js | |
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: | |
```js | |
function addThree (num) { | |
var result; | |
result = num + 3; | |
return result; | |
} | |
``` | |
So, when we make the function call of: | |
```js | |
addThree(10); | |
``` | |
You're calling the function `addThree` You're also passing a value `10` as an argument. | |
In the function declaration of `function addThree (num) {` You see that there is a parameter defined of `num` | |
When we do `addThree(10)` then the value of the parameter `num` is passed the argument value of ... `10` | |
Then if you follow the code through.. `result = num + 3;` ... and we know the value of `num` is `10`. | |
Therefore, if we follow through the function we end up with ... `result = 10 + 3;` then `result = 13;` then we return the `result`. | |
... | |
If you then make another function call.. | |
```js | |
addThree(39); | |
``` | |
You can use the same function to follow the operation: | |
When we do `addThree(39)` then the value of the parameter `num` is passed the argument value of ... `39` | |
Then if you follow the code through.. `result = num + 3;` ... and we know the value of `num` is now `39`. | |
Therefore, if we follow through the function we end up with ... `result = 39 + 3;` then `result = 42;` then we return the `result`. | |
If you make the function call of `addThree(21)` the value of `num` inside the function is 21. | |
If you make the function call of `addThree(1000)` the value of `num` inside the function is 1000. | |
If you make the function call of `addThree(123456)` the value of `num` inside the function is 123456. | |
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** | |
There is the function declaration of `function convertToF(celsius) {` .. You have a function name of `convertToF` and a parameter of `celsius` | |
Parameters are used just like variables. So, inside of the function you can use `celsius` JUST like any other variable. | |
If I have this code: | |
```js | |
function addThree (num) { | |
var result; | |
result = num + 3; | |
return result; | |
} | |
``` | |
So, when we make the function call of: | |
```js | |
addThree(10); | |
``` | |
You're calling the function `addThree` You're also passing a value `10` as an argument. | |
In the function declaration of `function addThree (num) {` You see that there is a parameter defined of `num` | |
When we do `addThree(10)` then the value of the parameter `num` is passed the argument value of ... `10` | |
Then if you follow the code through.. `result = num + 3;` ... and we know the value of `num` is `10`. | |
Therefore, if we follow through the function we end up with ... `result = 10 + 3;` then `result = 13;` then we return the `result`. | |
... | |
If you then make another function call.. | |
```js | |
addThree(39); | |
``` | |
You can use the same function to follow the operation: | |
When we do `addThree(39)` then the value of the parameter `num` is passed the argument value of ... `39` | |
Then if you follow the code through.. `result = num + 3;` ... and we know the value of `num` is now `39`. | |
Therefore, if we follow through the function we end up with ... `result = 39 + 3;` then `result = 42;` then we return the `result`. | |
If you make the function call of `addThree(21)` the value of `num` inside the function is 21. | |
If you make the function call of `addThree(1000)` the value of `num` inside the function is 1000. | |
If you make the function call of `addThree(123456)` the value of `num` inside the function is 123456. | |
function declaration of `function convertToF(celsius) { }` making the function call `convertToF(55)` the parameter `celsius` inside of the function has a value of 55 | |
function declaration of `function convertToF(celsius) { }` making the function call `convertToF(23)` the parameter `celsius` inside of the function has a value of 23 | |
The algorithm to convert from Celsius to Fahrenheit is the temperature in Celsius times 9/5, plus 32 | |
`Fahrenheit is the temperature in Celsius times 9/5, plus 32` | |
`Fahrenheit is Celsius times 9/5, plus 32` | |
`Fahrenheit is Celsius times 9/5 + 32` | |
`Fahrenheit is Celsius * 9/5 + 32` | |
.... | |
So you have a function declaration of `function checkObj(checkProp) { }` .. With that function declaration you have a function name of `checkObj` and you have a parameter of `checkProp`. | |
Parameters are treated like `variables` that represent the values that get passed into your function from the function call (arguments). | |
So, when you make a function call: | |
`checkObj("pet")` ... the parameter `checkProp` gets the value of the arguement `"pet"`. | |
`checkObj("horse")` ... the parameter `checkProp` gets the value of the arguement `"horse"`. | |
As mentioned above, you can then use the parameter inside of the function just like you'd use any other variable that holds a value. | |
Now, the lesson is trying to teach you about the object method `.hasOwnProperty()` | |
> The `hasOwnProperty()` method returns a boolean (`true`/`false`) indicating whether the object has the specified property as own (not inherited) property. | |
You could solve this: | |
```js | |
function checkObj(checkProp) { | |
if (myObj.hasOwnProperty(checkProp)) { | |
return myObj[checkProp]; | |
} else { | |
return "Not Found"; | |
} | |
} | |
``` | |
Like this: | |
```js | |
function checkObj(checkProp) { | |
return (myObj.hasOwnProperty(checkProp)) ? myObj[checkProp] : "Not Found"; | |
} | |
``` | |
Both of those solutions do the same thing. But, you have to know what a ternary operator is and so do the other developers looking at your code. | |
Why would you do this: | |
```js | |
function returnThing () { | |
var thing = "This is a thing that I need to return."; | |
return thing; | |
} | |
``` | |
Instead of just doing: | |
```js | |
function returnThing () { | |
return "This is a thing that I need to return."; | |
} | |
``` | |
Sure, later you'll have to extract complex code bits | |
but, for someone looking at your code. | |
They'll have to go... Okay what are we returning? `thing` | |
Okay, what is `thing` | |
Okay. So, that's what we're returning. | |
As your code gets more complex and sometimes even simpler. | |
You need to focus on readability and a process of understanding. | |
Making someone bounce around in your code to find what is happening can get confusing. | |
```js | |
function returnThing() { | |
var thisThing = "String Thing"; | |
var thatThing = thisThing; | |
var hisThing = thatThing; | |
var herThing = hisThing; | |
return herThing; | |
} | |
``` | |
function myFunc(name) { | |
name = name || "None Provided" | |
var x = val; | |
console.log("The value of x is: " + x); | |
var val = "value"; | |
console.log("The value of val is: " + val); | |
x = val; | |
console.log("The value of x is: " + x); | |
return name; | |
} | |
myFunc("Happy"); | |
```js | |
function unknownNumberOfArgs () { | |
console.log(arguments.length); | |
console.log(arguments); | |
var args = Array.from(arguments); | |
var args = [...arguments]; | |
return args; | |
} | |
unknownNumberOfArgs(1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6); | |
``` | |
```js | |
function someArguments (name) { | |
console.log(name); | |
console.log(arguments); | |
return "Hello " + name; | |
} | |
someArguments("Happy", "Feet", "has", "rhythm", "soul", "groove", "and", "when", "he", "starts", "to", "tap", "his", "feet.", "He", "really", "shakes", "his", "tail", "feather"); | |
``` | |
["a","b","c","d"].forEach(function (x) { console.log(x); console.log(arguments); return x;}) | |
```js | |
[true, true, false, false, true, false, false, true, true].filter(function (x) { return x }); | |
[23,1,6,12,17,3,2,5,19,86,87].filter(function (x) { return x % 2 === 0 }); | |
``` | |
```js | |
[23,1,6,12,17,3,2,5,19,86,87].filter(function (x) { console.log(arguments); return x % 2 === 0 }); | |
``` | |
https://repl.it/FBrn | |
```js | |
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18].filter(function (x) { | |
console.log("Arg[0]: " + arguments[0]) | |
console.log("x is: " + x); | |
console.log("Iteration: " + arguments[1]); | |
console.log("The Array: " + arguments[2]); | |
console.log("All Arguments:") | |
console.log(arguments); | |
console.log("---------") | |
return x % 2 === 0} | |
); | |
``` | |
```js | |
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18].filter(function (el , index , arr) { | |
console.log("Value of el is: " + el); | |
console.log("Element being processed: " + arguments[0]) | |
console.log("Index of the element: " + arguments[1]); | |
console.log("All Arguments:") | |
console.log(arguments); | |
console.log("---------") | |
return el % 2 === 0; | |
} | |
); | |
``` | |
```js | |
function recursion(num) { | |
if (num <= 0) return 0; | |
return (num + recursion(num - 1)); | |
} | |
recursion(11); | |
``` | |
```js | |
function recursion(num) { | |
return num && (num + recursion(num - 1)); | |
} | |
``` | |
I don't gift away answers. Studies have shown that giving people answers to problems instills an illusionary sense of learning. Because, it only puts information into short-term memory. Of which people think they are learning. Only through a little struggle and repetition that the human brain puts things into long-term memory. I don't know what your end goal. I don't know if you want to do this as a personal project, hobby, or if you want to move into industry. Because of this, I strongly feel that working with people to allow them to solve problems on their own is a far better outcome than giving away answers. But, others clearly don't agree with my methods for helping people learn. | |
You have to assign `'FirstLine newline backslash SecondLine backslash carriage-return ThirdLine'` to the variable myStr. | |
But, You have to replace the escape words with the proper escape sequences. (And per the instructions: remove all spaces.) | |
How do you escape a `newline`? With: `\n` | |
How do you escape a `backslash`? With: `\\` | |
How do you escape a `carriage-return`? With `\r` | |
So, you have break the problem down. | |
`FirstLine newline backslash SecondLine backslash carriage-return ThirdLine` | |
`FirstLine \n backslash SecondLine backslash carriage-return ThirdLine` | |
`FirstLine \n \\ SecondLine backslash carriage-return ThirdLine` | |
`FirstLine \n \\ SecondLine \\ carriage-return ThirdLine` | |
`FirstLine \n \\ SecondLine \\ \r ThirdLine` | |
You have to follow the instructions and remove all of the spaces | |
Then assign that to myStr as a string. | |
This lesson is not trying to teach you how to get some green check marks to pass onto the next lesson. It is trying to teach you a really valuable skill. If you have a string in JavaScript that is instantiated with double quotes (`"`) (Example: `var myStr = "Hello World";`) you can also instantiate a string with single quotes (`'`) (Example: `var myStr = 'Hello World';`) | |
How do you add a `"` to that string? Let's say you instantiated the string with double quotes and you want to put in a quote that someone said. You have to use an escape character. | |
Example: `var myQuote = "Happy Feet said, \"I really like the rhythm.\"";` | |
Or what happens if you want to add a tab to your string? You can't really add 3 spaces and expect it to show up as a tab. You have to use the escape character for a tab (`\t`). | |
OR let's say your trying to output a bunch of text and paragraphs within your JavaScript string. You can't really just make a bunch of variables. | |
`var myParagraph1 = "a bunch of characters";` | |
`var myParagraph2 = "a bunch more characters";` | |
I mean you could... but, why not just: | |
`var myStr = "A bunch of characters/sentences.\nThe second paragraph of characters and sentences.";` | |
I strongly feel that a lot of people are just trying to complete lessons and not trying to figure out what the lesson is actually trying to teach them. Because what the lesson is trying to teach is FAR more important of a concept than getting the lesson correct. | |
If I wanted to make a string for a variable myQuote... | |
Let's say this was my quote: `Happy Feet said, "I really like the rhythm."` | |
I would have to create the variable: | |
```js | |
var myQuote; | |
``` | |
I would then have to instantiate that variable as a string: | |
```js | |
var myQuote = ""; | |
``` | |
Now I add the string text into the variable. | |
```js | |
var myQuote = "Happy Feet said, "I really like the rhythm.""; | |
``` | |
Then because I have instantiated the string with a double quote. I have to escape the quotes inside of the string. | |
```js | |
var myQuote = "Happy Feet said, "I really like the rhythm.""; | |
``` | |
becomes: | |
```js | |
var myQuote = "Happy Feet said, \"I really like the rhythm.\""; | |
``` | |
Do you see how that works? | |
What is the returned value of `num` ... `var num = 4 + 3;` | |
What is the returned value of `num` ... `var num = 10 / 5;` | |
What is the returned value of `num` ... `var num = 5 * 3;` | |
**Remainder:** | |
http://images.tutorvista.com/cms/images/38/remainder1.PNG | |
`13 % 2` is `1` ... `26 % 5` is `1` ... `24 % 6` is `0` | |
`24 % 5` is `4` ... `219 % 20` is `19` | |
Just like in my examples above you use the `%` operator just like any other math operator (`+`, `-`, `*`, `/`) | |
Variables hold values. | |
The variable `playerNumber` is currently holding the value `16`. | |
You can use that variable (`playerNumber`) when referencing that value (of `16` in this case). | |
Instead of doing: `var player = testObj[16]; // Change this Line` ... You can use the variable `playerNumber` to reference the value that the variable `playerNumber` holds instead of hard coding the value `16` in: `testObj[16];` | |
What is the object that you're trying to test the property of? | |
What is the method for seeing of an object has a property? | |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty | |
The hasOwnProperty() method returns a boolean indicating whether the object has the specified property. | |
Let's say you were at a big business event and you're quite popular (with all those skills you have) throughout the event you got handed 200 business cards (Which will remain unordered). 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? | |
So, you're saying that after you ~~loop~~ look through all of the ~~contacts~~ cards in your ~~array of object~~ stack. That you'd tell me you don't have one for the individual? | |
`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. | |
```js | |
// code before a for loop. | |
for (..........) { | |
// Code inside of a for loop. | |
} | |
// code after a for loop. | |
``` | |
```js | |
for ( ..... ) { | |
if (evaluation) { | |
return "truth"; | |
} else { | |
return "lies"; | |
} | |
} | |
``` | |
This **exact** loop.. Does not loop. Because as we know: | |
The return statement ends function execution and specifies a value to be returned to the function caller. | |
*Syntax* | |
`return [[expression]]; ` | |
`expression` | |
The expression to return. If omitted, `undefined` is returned instead. | |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return | |
```js | |
for (var i = 0; i < 1000000; i++) { | |
if (i === 10) { | |
return 10; | |
} | |
else { | |
return "Not 10"; | |
} | |
} | |
``` | |
This loop will NOT lop. Because... | |
And IF something isn't true.. It is false... | |
In this case the else statement is a `return` value... SOOO.. It not only halts the loop but, It halts the whole function. | |
Let's say you have an object. | |
```js | |
// var name = "Happy"; | |
var myFriend = { | |
"firstName": "Happy", | |
"lastName": "Feet", | |
"number": "-i", | |
"likes": ["rhythm", "dancing", "soul"] | |
} | |
``` | |
How do **you** access the "firstName" of `myFriend`? | |
What does myFriend[name] output? | |
Is there a "Happy" property? | |
[Gist by revisualize / FreeCodeCamp - Nesting For Loops.js](https://gist.github.com/revisualize/fdc7f63fccdfd83739d33307b6dbc453) | |
`var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]];` | |
`myArray[0]` is `[1,2,3]` | |
`myArray[0][0]` is `1` | |
`myArray[0][1]` is `2` | |
`myArray[0][2]` is `3` | |
`myArray[1]` is `[4,5,6]` | |
`myArray[1][0]` is `4` | |
`myArray[1][1]` is `5` | |
`myArray[1][2]` is `6` | |
`myArray[2]` is `[7,8,9]` | |
`myArray[2][0]` is `7` | |
`myArray[2][2]` is `9` | |
`myArray[3]` is `[[10,11,12], 13, 14]` | |
`myArray[3][0]` is `[10,11,12]` | |
`myArray[3][0][0]` is `10` | |
`myArray[3][0][1]` is `11` | |
`myArray[3][0][2]` is `12` | |
`myArray[3][1]` is `13` | |
`myArray[3][2]` is `14` | |
```js | |
var arr = [ [ "a1" ] , [ "b2" , "c2" ] , [ "d3" , "e3" , "f3" ] , [ [ "g4" , "h4" ] , "i4" , "k4" ] ]; | |
``` | |
`arr[3]` is an array that contains 3 elements. The 0 index in the array is an array that contains two elements, `"g4"` & `"h4"` (`[ "g4" , "h4" ]`). The 1 index in `arr[3]` is a string value of `"i4"` and the 2 index in `arr[3]` is also a string value of `"k4"` ... The whole `arr[3]` is the value of `[ [ "g4" , "h4" ] , "i4" , "k4" ]` | |
```js | |
var arr = [ | |
[ "a1" ] , | |
[ "b2" , "c2" ] , | |
[ "d3" , "e3" , "f3" ] , | |
[ [ "g4" , "h4" ] ,"i4" , "k4" ] | |
]; | |
``` | |
```js | |
var arr = [ | |
[ "a1" ] , | |
[ "b2" , "c2" ] , | |
[ "d3" , "e3" , "f3" ] , | |
[ | |
[ "g4" , "h4" ] , | |
"i4" , | |
"k4" | |
] | |
]; | |
``` | |
```js | |
var arr = [ // this is where the parent array starts. | |
[ "a1" ] , // this is arr[0] | |
[ "b2" , "c2" ] , // this is arr[1] | |
[ "d3" , "e3" , "f3" ] , // this is arr[2] | |
[ // this is where arr[3] starts | |
[ "g4" , "h4" ] , // this is arr[3][0] | |
"i4" , // this is arr[3][1] | |
"k4" // this is arr[3][2] | |
] // this is where arr[3] ends | |
]; // This is where the parent array ends | |
``` | |
[Gist by revisualize / FreeCodeCamp - Profile Lookup.js](https://gist.github.com/revisualize/4c38d3548ff935ee28e945c81dfe652d) | |
We have an array of objects representing different people in our contacts lists. | |
Example: var contacts = [ { ... } , { ... } , { ... } , { ... } ]; | |
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 of objects: | |
```js | |
var contacts = [ { f: "A" } , { f: "H" } , { f: "S" } , { f: "K" } ]; | |
``` | |
How do you output `"H"`? | |
What if `f:` was `firstName:` ? | |
If I was to do this example where I was looking for a letter in an array of strings. | |
If the letter is found return the string "True" otherwise return the string "False" | |
```js | |
var value = "e" | |
var arr = ["a", "b", "c", "d", "e"]; | |
for (var i = 0; i < arr.length; i++) { | |
if (value === arr[i]) { return "True"; } | |
} | |
return "False"; | |
``` | |
Then if you redid that challenge with the letter "z" as the value of the variable value | |
If you're looking for a value in an array and you do this: | |
``` | |
var value = "e" | |
var arr = ["a", "b", "c", "d", "e"]; | |
for (var i = 0; i < arr.length; i++) { | |
// at the first iteration i is 0 | |
// arr[i] is arr[0] and arr[0] holds the value of "a" | |
// if ("e" === "a") ... return "True" ... Well, "e" is not "a" so, no. | |
if (value === arr[i]) { return "True"; } | |
// okay so next block | |
// if "e" is not equal to "a" ... that's true ... so ... return "False" | |
else if (value !== arr[i]) { return "False" } | |
} | |
``` | |
Your loop won't loop because the return statement is triggered prior to the element actually being found. | |
(Yes, you could use `.indexOf()` but, this is an example.) | |
function findElement (el, arr) { | |
for (var i = 0; i < arr.length; i++) { // this allows us to iterate across the array. | |
if (el === arr[i]) { | |
return true; | |
} | |
} | |
return false; | |
} | |
var arr = ["a","c","e","g","i"]; | |
// ^-----------___ | |
findElement("b" , arr); | |
[Gist by revisualize / FreeCodeCamp - Record Collection.js](https://gist.github.com/revisualize/123deb77d85079f079020ac3cd6ea5ba) | |
[Gist by revisualize / FreeCodeCamp - Stand in Line.js](https://gist.github.com/revisualize/ced4a3a6611c6c74bcab34a07eaa4ebf) | |
// How do **you** add a number (`item`) to the end of an array (`arr`)? What does this method return? | |
// How do **you** remove the first element from an array (`arr`)? What does this method return? | |
You're supposed to: | |
Write a function nextInLine which has two parameters | |
an array (`arr`) and a number (`item`). | |
That part has been done for you here: | |
```js | |
function nextInLine(arr, item) { } | |
``` | |
Parameters are treated like `variables` that represent the values that get passed into your function from the function call (arguments). | |
Here's an example of this: | |
``` js | |
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. | |
You just need to add a number (`item`) to the end of an array (`arr`). | |
You also need to remove the first element from an array (`arr`). | |
But, there is another part of this challenge. You need to know What the methods that do those two things `return`. | |
The `push()` method adds one or more elements to the end of an array **and** returns the new length of the array. | |
The `pop()` method removes the last element from an array **and** returns that element. | |
The `unshift()` method adds one or more elements to the beginning of an array **and** returns the new length of the array. | |
The `shift()` method removes the first element from an array **and** returns that element. | |
To understand what this is doing you should maybe answer this question: | |
```js | |
function getName() { return "Happy Feet" } | |
var name = getName(); | |
console.log(name); | |
``` | |
What do **you** think is output to the console when you log the variable `name`? | |
Why do you think that is? | |
Then you go back through and figure out what you're doing and how to **do it**. | |
[Gist by revisualize / FreeCodeCamp - Stand in Line.js](https://gist.github.com/revisualize/ced4a3a6611c6c74bcab34a07eaa4ebf) | |
You're supposed to: | |
Write a function nextInLine which has two parameters | |
an array (`arr`) and a number (`item`). | |
That part has been done for you here: | |
```js | |
function nextInLine(arr, item) { } | |
``` | |
Parameters are treated like `variables` that represent the values that get passed into your function from the function call (arguments). | |
You just need to add a number (`item`) to the end of an array (`arr`). | |
You also need to remove the first element from an array (`arr`) **and** you need to return the element that was removed. | |
So, How do **you** add a number (`item`) to the end of an array (`arr`)? | |
How do **you** remove the first element from an array (`arr`)? | |
... | |
Here's a brief example about how to use parameters: | |
``` js | |
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. | |
Stand In Line is a CheckPoint where you're supposed to: | |
Write a function `nextInLine` which has two parameters | |
an array (`arr`) and a number (`item`). | |
That part has been done for you here: | |
```js | |
function nextInLine(arr, item) { } | |
``` | |
With that function declaration you have a function name of `nextInLine`. You have two parameters `arr` which represents an array passed into the function and `item` that represents a number passed to the function. | |
A few example function calls: | |
`nextInLine([4,3,2] , 1)` the values passed to `arr` is `[4,3,2]` and the value passed to `item` is `1` | |
`nextInLine([2,4,6,8] , 10)` ... `arr` is `[2,4,6,8]` & `item` is `10` | |
`nextInLine([2,3,5,7,11] , 13)` ... `arr` is `[2,3,5,7,11]` & `item` is `13` (first 5 and 6th prime numbers) | |
`nextInLine([] , 0)` ... `arr` is `[]` & `item` is `0` | |
This part is an example using a Global Variable as a parameter to another function call. | |
`var myArr = [1,3,5,7]; nextInLine(myArr , 9)` ... `arr` is a reference to the global variable `myArr` with the value of `[1,3,5,7]` & `item` is `9` | |
When you modify `arr` inside of your function you're actually modifying the value of the global variable because of the reference passing. Note: This doesn't work like this when you pass primitives. | |
Parameters are treated like `variables` that represent the values that get passed into your function from the function call (arguments). | |
Again the two parameters for the `nextInLine` function are `arr` & `item`. | |
Per the instructions: Add the number to the end of the array, then remove the first element of array. The `nextInLine` function should then return the element that was removed. | |
You need to add the number (`item`) to the end of an array (`arr`). | |
You also need to remove the first element from an array (`arr`). | |
Then you need to have your function `return` the removed element from the array `arr`. | |
[Gist by revisualize / FreeCodeCamp - Word_Blanks.js](https://gist.github.com/revisualize/3fb92987929aa5932c0bf33d229b28f6) | |
The function declaration was set up as... `function wordBlanks(myNoun, myAdjective, myVerb, myAdverb)` giving the function name as `wordBlanks` and with four parameter of `myNoun`, `myAdjective`, `myVerb` and `myAdverb`. | |
There is more that I could go into (if you want I can) but, I'll sum it up with: | |
Parameters are treated like variables that represent the values that get passed into your function from the function call (arguments). | |
Here's an example of this: | |
``` js | |
function hello(fName, uName) { | |
var result; | |
result = "Hello " + fName + " " + uName + ", How is your day?"; | |
return result; | |
} | |
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. | |
Now this example only uses two parameters `fName` and `uName`. Whereas, `wordBlanks` has four parameters. | |
When you have the function declaration of: | |
```js | |
function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) { } | |
``` | |
and you make the function call of: | |
`wordBlanks("dog", "big", "ran", "quickly");` | |
The parameter `myNoun` gets the first argument from the function call of a string `"dog"` | |
The parameter `myAdjective` gets the second argument from the function call of a string `"big"` | |
The parameter `myVerb` gets the third argument from the function call of a string `"ran"` | |
The parameter `myAdverb` gets the second argument from the function call of a string `"quickly"` | |
Parameters are treated like variables that represent the values that get passed into your function from the function call (arguments). | |
So, you need to use the parameters to create a string and assign that string value to the variable `result`. (String Concatenation) | |
If you are posting code that is large, Please use Gist - https://gist.github.com/ then please paste the link here. | |
... | |
**How to do code block format:** ..... PLEASE try with a small block of code before you try to make a bigger one. | |
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code | |
\`\`\`js **[SHIFT+ENTER]** | |
// your code here | |
\`\`\` **[CTRL+ENTER or CMD+ENTER to send]** | |
Please do not confuse \`s with 's. | |
Also note that the \`\`\`s need to each be on their own lines | |
The \` key can usually be found on most keyboards up next to the 1 key. | |
If you hit SHIFT+\` it will give you the ~. | |
Example: | |
\`\`\` | |
code | |
\`\`\` | |
... | |
Output: | |
```js | |
var code = undefined; | |
``` | |
... | |
You can also do inline code formatting: | |
A string of text with a \`variable\` or \`other bit of code\`. | |
Output: | |
A string of text with a `variable` or `other bit of code`. |
revisualize
commented
Nov 9, 2016
•
ECMAScript standard states it clearly that function parameters and variables live in the same Environmental record in the execution context:
ECMAScript standard explains it roughly like this (10. Executable Code and Execution Contexts)
10.3... When control is transferred to ECMAScript executable code, control is entering an execution context (global code, eval code, or function code).
An execution context contains whatever state is necessary to track the execution progress of its associated code. In addition, each execution context has:
- LexicalEnvironment: Identifies the Lexical Environment used to resolve identifier references made by code within this execution context.
- VariableEnvironment: Identifies the Lexical Environment whose environment record holds bindings created by VariableStatements and FunctionDeclarations within this execution context.
- ThisBinding: The value associated with the this keyword within ECMAScript code associated with this execution context.
When control enters an execution context, the execution context‘s ThisBinding is set, its VariableEnvironment and initial LexicalEnvironment are defined, and declaration binding instantiation (10.5) is performed.
10.5... Every execution context has an associated VariableEnvironment. Variables and functions declared in
ECMAScript code evaluated in an execution context are added as bindings in that VariableEnvironment‘s
Environment Record. For function code, parameters are also added as bindings to that Environment Record.
Look, I'm going to add my two cents to this whole Github question:
Should I store my work in Github or on freeCodeCamp or in CodePen?
You should put ALL of your work on Github. You can have a Codepen you can have your freeCodeCamp profile. But, EVERYTHING should be moved to your Github account as a centralized location to direct an employer to when they are looking to see your work. There are a LOT of HR people that if they don't see activity on your github they don't think you're a dedicated developer. Every recruiter that I've talked to states that they don't know how to code they don't understand what they're looking at they just want to see if you're active. They will then forward your Github account to someone on a team to review before sending the details onto the hiring manager.
I also have a friend that is a CEO for a startup and she works with other smaller companies to do IT focused hiring. She will not hire a developer if they do not have a well flushed out Github profile. She won't even bother looking at their resume if they don't include a link to their github. SO... all of your work should be on Github if you want to work in this industry.
The JS engine parses the code (lexical analysis) and turns it into bytecode that the compiler can then execute (JIT processes). Some engines (V8) also compile JS source code into machine code.