# data[].attributes.body
Bracket notation vs dot notation

var user = {
  email: "user@example.com",
  firstName: "first",
  lastName: "last"
};

BAD PRACTICES

don't use numbers as attribute name:

1stChild otherwise you will get problem when using bracket notation

don't use " to your attribute names like "email" : "user@lala.com"

don't use hyphens attribute names

don't use spaces as attribute names

# data[].attributes.body
object { Key: value }

You can define objects using object-literal notation:

umbrella{
color: "pink",
isOpen: false,
open : function(){
if (umbrella.isOpen === true){
return "Already open";
} else {
umbrella.isOpen = true;
return "Please, open the umbrella"  }
}
};
==============================================

var sister = {
  name: "Sarah",
  age: 23,
  parents: [ "alice", "andy" ],
  siblings: ["julia"],
  favoriteColor: "purple",
  pets: true,
  paintPicture: function() { return "Sarah paints!"; }
};

sister.name

> Sarah

typeof umbrella
>> object

# data[].attributes.body
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Accessor_methods
These methods do not modify the array and return some representation of the array.

var turnMeIntoAString = ["U", "d", "a", "c", "i", "t", "y"];


turnMeIntoAString.join('');



Returns: Udacity

LOOPS
forEach() method can take up to three parameters: element, index, array

words = ["cat", "in", "hat"];
words.forEach(function(word, num, all) {
  console.log("Word " + num + " in " + all.toString() + " is " + word);
});

Prints:
Word 0 in cat,in,hat is cat // num = 0 (index), all: cat,in,hat (array), word: (element)
Word 1 in cat,in,hat is in
Word 2 in cat,in,hat is hat

MAPS (creates a new array)

var donuts = ["jelly donut", "chocolate donut", "glazed donut"];

var improvedDonuts = donuts.map(function(donut) {
  donut += " hole";
  donut = donut.toUpperCase();
  return donut;
});

donuts array: ["jelly donut", "chocolate donut", "glazed donut"]
improvedDonuts array: ["JELLY DONUT HOLE", "CHOCOLATE DONUT HOLE", "GLAZED DONUT HOLE"]

# data[].attributes.body
Properties: length
Methods to modify arrays:
push() : You can use the push() method to add elements to the end of an array
pop() : Alternatively, you can use the pop() method to remove elements from the end of an array.
reverse(), short()
shift() will remove the first element from an array.
splice() : is another handy method that allows you to add and remove elements from anywhere within an array.

Exemple:

donuts.splice(1, 1, "chocolate cruller", "creme de leche");

If negative, will begin that many elements from the end of the array (with origin -1) and will be set to 0 if absolute value is greater than the length of the array.

var donuts = ["cookies", "cinnamon sugar", "creme de leche"];

donuts.splice(-2, 0, "chocolate frosted", "glazed");

>>  ["cookies", "chocolate frosted", "glazed", "cinnamon sugar", "creme de leche"]

Remove 1 element of index 3

var removed = myFish.splice(3, 1);

Quizz Example

1. Remove "Blackberry"

2. Add "Yellow" and "Green"

3. Add "Purple"

var rainbow = ["Red", "Orange", "Blackberry", "Blue"];

rainbow.splice(2, 1);
rainbow.splice(3, 2, "Yellow", "Green");
rainbow.splice(rainbow.length, 1, "Purple");

console.log(rainbow);

# data[].attributes.headline
Function expressions // Patterns // Example Named functions

# data[].attributes.subject
Function expressions // Patterns // Example Named functions

# data[].attributes.body
// named function expression
var doSomething = function addOne(y) {
  return y + 1;
};

// for either of the definitions above, call the function like this:
doSomething(5);

# data[].attributes.headline
Function Expressions // Patterns // Example pass a function Inline 

# data[].attributes.subject
Function Expressions // Patterns // Example pass a function Inline 

# data[].attributes.body
// function declaration that takes in two arguments: a function for displaying
// a message, along with a name of a movie
function movies(messageFunction, name) {
  messageFunction(name);
}

// call the movies function, pass in the function and name of movie
movies(function displayFavorite(movieName) {
  console.log("My favorite movie is " + movieName);
}, "Finding Nemo");

Quizz example
// don't change this code
function emotions(myString, myFunc) {
console.log("I am " + myString + ", " + myFunc(4));
}

// your code goes here
emotions("happy", function laugh(num){
var _text = "";
for(var i =0; i<num; i++){
_text += "ha";
}
return _text + "!";
});

# data[].attributes.headline
Function Expression // Patterns // Example anonymous function

# data[].attributes.subject
Function Expression // Patterns // Example anonymous function

# data[].attributes.body
var laugh = function(num){
var text = "ha";
var ending = "!";
var _text = "";
for(var i =0; i<num; i++){
_text += text;
}
return _text + ending;
}

console.log(laugh(10));

>> hahahahahahahahahaha!

# data[].attributes.body
// Function expression that assigns the function displayFavorite
// to the variable favoriteMovie
var favoriteMovie = function displayFavorite(movieName) {
  console.log("My favorite movie is " + movieName);
};

// Function declaration that has two parameters: a function for displaying
// a message, along with a name of a movie
function movies(messageFunction, name) {
  messageFunction(name);
}

// Call the movies function, pass in the favoriteMovie function and name of movie
movies(favoriteMovie, "Finding Nemo");

>> My favorite movie is Finding Nemo

OR ========================================

// Function declaration that takes in two arguments: a function for displaying
// a message, along with a name of a movie
function movies(messageFunction, name) {
  messageFunction(name);
}

// Call the movies function, pass in the function and name of movie
movies(function displayFavorite(movieName) {
  console.log("My favorite movie is " + movieName);
}, "Finding Nemo");

# data[].attributes.body
A function that is passed into another function is called a callback

// function expression catSays
var catSays = function(max) {
  var catMessage = "";
  for (var i = 0; i < max; i++) {
    catMessage += "meow ";
  }
  return catMessage;
};

// function declaration helloCat accepting a callback
function helloCat(callbackFunc) {
  return "Hello " + callbackFunc(3);
}

// pass in catSays as a callback function
helloCat(catSays);

>> "Hello meow meow meow "

Anonymous inline expressions

Using an anonymous inline function expression might seem like a very not-useful thing at first. Why define a function that can only be used once and you can't even call it by name?

Anonymous inline function expressions are often used with function callbacks that are probably not going to be reused elsewhere. Yes, you could store the function in a variable, give it a name, and pass it in like you saw in the examples above. However, when you know the function is not going to be reused, it could save you many lines of code to just define it inline.

// anonymous function expression
var doSomething = function(y) {
  return y + 1;
};

// named function expression
var doSomething = function addOne(y) {
  return y + 1;
};

# data[].attributes.headline
Function expressions vs Function declaration // Hoisting

# data[].attributes.subject
Function expressions vs Function declaration // Hoisting

# data[].attributes.body
All function declarations are hoisted and loaded before the script is actually run. Function expressions are not hoisted, since they involve variable assignment, and only variable declarations are hoisted. The function expression will not be loaded until the interpreter reaches it in the script.

# data[].attributes.body
Now function (max) is called an anonymous function because it's no longer a function, it's stored inside a variable. To access to the function you need to call the variable.
The function can be named, or anonymous.

var catSays = function(max) {
  var catMessage = "";
  for (var i = 0; i < max; i++) {
    catMessage += "meow ";
  }
  return catMessage;
};

This can also happen when you pass a function inline as an argument to another function

# data[].attributes.body
JS programers usually declare functions at the top of their scripts, and variables at the top of the functions.
The variable declaration is hoisted to the top of current scope (the top of the function). Remember that the declaration is hoisted, not the assignment.

# data[].attributes.headline
Function Declaration // About return....

# data[].attributes.subject
Function Declaration // About return....

# data[].attributes.body
function sleep() {
  console.log("I'm sleepy!");
  return "zzz";
  return "snore";
}

sleep();

The first return statement will exit the function.

The second return statement will never be reached.

However, it is possible to have multiple return statements in a function, ex. if -> return, else -> return, etc.

# data[].attributes.headline
Function Declaration // Parameters vs arguments

# data[].attributes.subject
Function Declaration // Parameters vs arguments

# data[].attributes.body
parameters are defined in a function
arguments are passed to a function

# data[].attributes.body
for (var row = 0; row < 26; row += 1) {
for (var seat = 0; seat < 100; seat +=1){
console.log(row + '-' + seat);
}
}

# data[].attributes.body
conditional ? (if condition is true) : (if condition is false)

var color = isGoing ? "green" : "red";
console.log(color);

# data[].attributes.body
A value is falsy if it converts to false when evaluated in a boolean context. For example, an empty String "" is falsy because, "" evaluates to false. You already know if...else statements, so let's use them to test the truthy-ness of "".

if ("") {
    console.log("the value is truthy");
} else {
    console.log("the value is falsy");
}

Here’s the list of all of the falsy values:

1. the Boolean value false

2. the null type

3. the undefined type

4. the number 0

5. the empty string ""

6. the odd value NaN (stands for "not a number", check out the NaN MDN article)

# data[].attributes.body
"Then man whispered, \" please speak to me. \" "

\\ --> \
\" --> "
\' --> '
\t --> tab
\n --> new line

# data[].attributes.body
BAD PRACTICE if comparing numbers:
3 == 4
3 != 4

GOOD PRACTICE if comparing numbers:
3 === 4
"1" !== 1

# data[].attributes.body
false == 0
> true

false === 0
> false

# data[].attributes.body
when comparing variables in JS they are converted to the same type if possible.

Primitive Datatypes
strings, numbers, booleans, undefined, null

# data[].attributes.headline
Strongly typed programming vs loosely typed programm

# data[].attributes.subject
Strongly typed programming vs loosely typed programm

# data[].attributes.body
strongly:
int count = 1
string name = "Julia"
double num = 123.12

loosely:
var count = 1
var name = "Julia"
var num = 123.12

# data[].attributes.body
No need to specify datatypes since JSEngine convert to appropriate datastructure