├── .gitignore
├── index.js
├── 03 Operators
├── images
│ └── 01-01.png
├── 12- Exercise - Swapping Variables.js
├── 01- JavaScript Operators.md
├── 10- Operators Precedence.js
├── 03- Assignment Operators.js
├── 04- Comparison Operators.js
├── 06- Ternary Operator.js
├── 02- Arithmetic Operators.js
├── 05- Equality Operators.js
├── 07- Logical Operators.js
├── 11- Quiz.md
├── 08- Logical Operators with Non-booleans.js
└── 09- Bitwise Operators.js
├── 01 Getting Started
├── js-basics.zip
├── images
│ ├── 01-01.png
│ └── 01-02.png
├── 06- JavaScript in Node.md
├── 03- Setting Up the Development Environment.md
├── 05- Separation of Concerns.md
├── 02- What is JavaScript.md
└── 04- JavaScript in Browsers.md
├── 06 Arrays
├── 17- Exercise 1- Array from Range.js
├── 19- Exercise 3- Except.js
├── 22- Exercise 6- Get Max.js
├── 09- The Spread Operator.js
├── 18- Exercise 2- Includes.js
├── 14- Filtering an Array.js
├── 01- Introduction.js
├── 11- Joining Arrays.js
├── 06- Removing Elements.js
├── 20- Exercise 4- Moving an Element.js
├── 03- Finding Elements (Primitives).js
├── 21- Exercise 5- Count Occurrences.js
├── 16- Reducing an Array.js
├── 23- Exercise 7- Movies.js
├── 10- Iterating an Array.js
├── 15- Mapping an Array.js
├── 12- Sorting Arrays.js
├── 07- Emptying an Array.js
├── 05- Arrow Functions.js
├── 13- Testing the Elements of an Array.js
├── 04- Finding Elements (Reference Types).js
├── 02- Adding Elements.js
└── 08- Combining and Slicing Arrays.js
├── 05 Objects
├── 15- Exercise 1- Address Object.js
├── 18- Exercise 4- Blog Post Object.js
├── 19- Exercise 5- Constructor Functions.js
├── 20- Exercise 6- Price Range Object.js
├── 04- Dynamic Nature of Objects.js
├── 16- Exercise 2- Factory and Constructor Functions.js
├── 10- Garbage Collection.js
├── 11- Math.js
├── 08- Enumerating Properties of an Object.js
├── 03- Constructor Functions.js
├── 17- Exercise 3- Object Equality.js
├── 13- Template Literals.js
├── 09- Cloning an Object.js
├── 06- Functions are Objects.js
├── 01- Basics.js
├── 14- Date.js
├── 05- Constructor Property.js
├── 07- Value vs Reference Types.js
├── 02- Factory Functions.js
└── 12- String.js
├── index.html
├── 04 Control Flow
├── 10- Exercise 1- Max of Two Numbers.js
├── 11- Exercise 2- Landscape or Portrait.js
├── 19- Exercise 10- Stars.js
├── 16- Exercise 7- String Properties.js
├── 12- Exercise 3- FizzBuzz.js
├── 06- Infinite Loops.js
├── 17- Exercise 8- Sum of Multiples of 3 and 5.js
├── 15- Exercise 6- Count Truthy.js
├── 14- Exercise 5- Even and Odd Numbers.js
├── 08- For...of.js
├── 20- Exercise- Prime Numbers.js
├── 13- Exercise 4- Demerit Points.js
├── 07- For...in.js
├── 02- Switch...case.js
├── 04- While.js
├── 18- Exercise 9- Grade.js
├── 05- Do...while.js
├── 01- If...else.js
├── 09- Break and Continue.js
└── 03- For.js
├── 02 Basics
├── 02- Constants.js
├── 08- Types of Functions.js
├── 03- Primitive Types.js
├── 04- Dynamic Typing.js
├── 07- Functions.js
├── 06- Arrays.js
├── 05- Objects.js
└── 01- Variables.js
├── 07 Functions
├── 13- Exercise 2- Area of Circle.js
├── 12- Exercise 1- Sum of Arguments.js
├── 14- Exercise 3- Error Handling.js
├── 02- Hoisting.js
├── 03- Arguments.js
├── 04- The Rest Operator.js
├── 09- Let vs Var.js
├── 01- Function Declarations vs Expressions.js
├── 05- Default Parameters.js
├── 08- Local vs Global Scope.js
├── 06- Getters and Setters.js
├── 10- The this Keyword.js
├── 07- Try and Catch.js
└── 11- Changing this.js
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | console.log("hello")
--------------------------------------------------------------------------------
/03 Operators/images/01-01.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jmschp/mosh-ultimate-javascript-mastery-series-part-1/HEAD/03 Operators/images/01-01.png
--------------------------------------------------------------------------------
/01 Getting Started/js-basics.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jmschp/mosh-ultimate-javascript-mastery-series-part-1/HEAD/01 Getting Started/js-basics.zip
--------------------------------------------------------------------------------
/01 Getting Started/images/01-01.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jmschp/mosh-ultimate-javascript-mastery-series-part-1/HEAD/01 Getting Started/images/01-01.png
--------------------------------------------------------------------------------
/01 Getting Started/images/01-02.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jmschp/mosh-ultimate-javascript-mastery-series-part-1/HEAD/01 Getting Started/images/01-02.png
--------------------------------------------------------------------------------
/01 Getting Started/06- JavaScript in Node.md:
--------------------------------------------------------------------------------
1 | # 06- JavaScript in Node
2 |
3 | Open a terminal and navigate to the folder with the `.js` file
4 |
5 | In the command line run `node index.js`
6 |
--------------------------------------------------------------------------------
/06 Arrays/17- Exercise 1- Array from Range.js:
--------------------------------------------------------------------------------
1 | // 17- Exercise 1- Array from Range
2 |
3 |
4 | function arrayFromRange(min, max) {
5 | const numbersArray = new Array();
6 | for(let i = min; i <= max; i++) {
7 | numbersArray .push(i);
8 | }
9 | return numbersArray;
10 | }
11 |
12 | const numbers = arrayFromRange(-10, -0)
13 |
14 | console.log(numbers);
--------------------------------------------------------------------------------
/05 Objects/15- Exercise 1- Address Object.js:
--------------------------------------------------------------------------------
1 | // 15- Exercise 1- Address Object
2 |
3 | const address = {
4 | street: 'Rua Afonso Braz 505',
5 | city: 'São Paulo',
6 | zipCode: '04511-011'
7 | };
8 |
9 | function showAddress(address) {
10 | for (let key in address) {
11 | console.log(key, address[key])
12 | }
13 | };
14 |
15 | showAddress(address);
16 |
--------------------------------------------------------------------------------
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | The Ultimate JavaScript Mastery Series - Part 1
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/04 Control Flow/10- Exercise 1- Max of Two Numbers.js:
--------------------------------------------------------------------------------
1 | // 10- Exercise 1- Max of Two Numbers
2 |
3 | // Write a function that takes two numbers and returns the maximum of the two.
4 |
5 | function max1(number1, number2) {
6 | return (number1 > number2) ? number1 : number2;
7 | }
8 |
9 | console.log(max1(4, 9))
10 |
11 | // We could also use if else statements, but this is the cleaner implementation.
--------------------------------------------------------------------------------
/02 Basics/02- Constants.js:
--------------------------------------------------------------------------------
1 | // 02 Constants
2 |
3 |
4 |
5 | // In situations where we don't want the value of our variable to change we use constant
6 |
7 | let interestRateVariable = 0.3;
8 | interestRateVariable = 0.5;
9 |
10 | const interestRateConstant = 0.5;
11 | // interestRateConstant = 0.3; if we try to change the value of a constant this will raise na error
12 | console.log(interestRateConstant);
13 |
--------------------------------------------------------------------------------
/03 Operators/12- Exercise - Swapping Variables.js:
--------------------------------------------------------------------------------
1 | // 12- Exercise - Swapping Variables
2 |
3 | /*
4 | Swap the value of two variables
5 | We initiate two variable "a" and "b", each with its value.
6 | The we mus swap their value.
7 | */
8 |
9 | let a = 'red';
10 | let b = 'blue';
11 |
12 | console.log(a);
13 | console.log(b);
14 |
15 | let c = a;
16 | a = b
17 | b = c
18 |
19 | console.log(a);
20 | console.log(b);
--------------------------------------------------------------------------------
/05 Objects/18- Exercise 4- Blog Post Object.js:
--------------------------------------------------------------------------------
1 | // 18- Exercise 4- Blog Post Object
2 |
3 | const blogPost = {
4 | title: 'How to become a dev',
5 | body: 'blablablabla',
6 | author: 'Miguel',
7 | views: 300,
8 | comments: [
9 | {author: 'John', body: 'blavbaddad'},
10 | {author: 'Angie', body: 'blavbaddad'},
11 | {author: 'Luis', body: 'blavbaddad'}
12 | ],
13 | isLive: true
14 | };
15 |
--------------------------------------------------------------------------------
/03 Operators/01- JavaScript Operators.md:
--------------------------------------------------------------------------------
1 | # 01- JavaScript Operators
2 |
3 | In JavaScript we different kinds of Operators. We use this operator with our variables to create expressions and with these expressions we can implement logic and Algorithms
4 |
5 | 
6 |
7 | Kinds of operators:
8 |
9 | - Arithmetic
10 | - Assignment
11 | - Comparison
12 | - Logical
13 | - Bitwise
14 |
--------------------------------------------------------------------------------
/06 Arrays/19- Exercise 3- Except.js:
--------------------------------------------------------------------------------
1 | // 19- Exercise 3- Except
2 |
3 |
4 | function except(array, excluded) {
5 | const output = [];
6 | array.forEach(element => {
7 | if (!excluded.includes(element)) {
8 | output.push(element);
9 | }
10 | });
11 | return output;
12 | }
13 |
14 | const numbers = [1, 1, 2, 3, 4, 1, 1];
15 |
16 | const newArray = except(numbers, [1, 3, 4]);
17 | console.log(newArray);
18 |
--------------------------------------------------------------------------------
/04 Control Flow/11- Exercise 2- Landscape or Portrait.js:
--------------------------------------------------------------------------------
1 | // 11- Exercise 2- Landscape or Portrait
2 |
3 | // Create a function that returns true if a images is in landscape format and false if is in portrait format.
4 |
5 |
6 | function isLandscape(width, height) {
7 | return width > height // ? true : false; we don't need this last parte because the evaluation of the condition is already returning true or false.
8 | }
9 |
10 | console.log(isLandscape(800, 600))
--------------------------------------------------------------------------------
/05 Objects/19- Exercise 5- Constructor Functions.js:
--------------------------------------------------------------------------------
1 | // 19- Exercise 5- Constructor Functions
2 |
3 |
4 | function Post(title, body, author, isLive=false) {
5 | this.title = title;
6 | this.body = body;
7 | this.author = author;
8 | };
9 |
10 |
11 |
12 | function Post(title, body, author) {
13 | this.title = title;
14 | this.body = body;
15 | this.author = author;
16 | this.views = 0;
17 | this.comments = [];
18 | this.isLive = false;
19 | };
--------------------------------------------------------------------------------
/03 Operators/10- Operators Precedence.js:
--------------------------------------------------------------------------------
1 | // 10- Operators Precedence
2 |
3 | /*
4 | Operators have precedence
5 | Just like in math the expression between the parentheses "()" are evaluated first.
6 | */
7 |
8 | let x = 2 + 4 * 3 // Here the * operator has higher precedence so 4 * 3 is calculates first and then add 2 so the result is 14
9 | console.log(x)
10 |
11 | let y = (2 + 3) * 4 // Here because we use parentheses in (2 + 4) this is calculates first and then we add 3 so the result is 20
12 | console.log(y)
--------------------------------------------------------------------------------
/07 Functions/13- Exercise 2- Area of Circle.js:
--------------------------------------------------------------------------------
1 | // 13- Exercise 2- Area of Circle.js
2 |
3 | const circle = {
4 | radius: 1,
5 | set circleRadius(rad) {
6 | this.radius = rad;
7 | },
8 | get circleArea() {
9 | return Math.PI * this.radius ** 2;
10 | }
11 | }
12 | circle.circleRadius= 10;
13 | console.log(circle.circleArea);
14 |
15 | // Mosh solution
16 |
17 | const circleMosh = {
18 | radius: 1,
19 | get area() {
20 | return Math.PI * this.radius * this.radius;
21 | },
22 | };
23 |
--------------------------------------------------------------------------------
/03 Operators/03- Assignment Operators.js:
--------------------------------------------------------------------------------
1 | // 03- Assignment Operators
2 |
3 | /*
4 | Theses are the assignment operator:
5 | =
6 | +=
7 | -=
8 | *=
9 | /=
10 | %=
11 | */
12 |
13 | let x = 10; // Assigning 10 to x
14 |
15 | console.log(x += 5); // This is the same as x = x + 5
16 | console.log(x -= 5); // This is the same as x = x - 5
17 | console.log(x *= 5); // This is the same as x = x * 5
18 | console.log(x /= 5); // This is the same as x = x / 5
19 | console.log(x %= 5); // This is the same as x = x % 5
--------------------------------------------------------------------------------
/05 Objects/20- Exercise 6- Price Range Object.js:
--------------------------------------------------------------------------------
1 | // 20- Exercise 6- Price Range Object
2 |
3 |
4 | const priceRange = [
5 | {minPrice: 0, maxPrice: 99},
6 | {minPrice: 100, maxPrice: 150},
7 | {minPrice: 151, maxPrice: 200}
8 | ]
9 |
10 |
11 | // Mosh solution
12 | let priceRange = [
13 | { label:'$', tooltip: 'Inexpensive', minPricePerPerson: 0, maxPricePerPerson: 10 },
14 | { label:'$', tooltip: 'Moderate', minPricePerPerson: 11, maxPricePerPerson: 20 },
15 | { label:'$', tooltip: 'Expensive', minPricePerPerson: 21, maxPricePerPerson: 50 },
16 | ];
17 |
--------------------------------------------------------------------------------
/06 Arrays/22- Exercise 6- Get Max.js:
--------------------------------------------------------------------------------
1 | // 22 - Exercise 6 - Get Max
2 |
3 | const numbers = [1, 5, 2, 3, 4];
4 |
5 | const max = getMaxMosh(numbers);
6 | console.log(max);
7 |
8 | function getMax(array) {
9 | return array.reduce((accumulator, currentValue) => {
10 | const max = (accumulator > currentValue) ? accumulator : currentValue
11 | return max
12 | }, undefined);
13 | }
14 |
15 | // Mosh solution
16 |
17 | function getMaxMosh(array) {
18 | if (array.length ===0) return undefined;
19 | return array.reduce((a, b) => (a > b) ? a : b);
20 | }
--------------------------------------------------------------------------------
/04 Control Flow/19- Exercise 10- Stars.js:
--------------------------------------------------------------------------------
1 | // 19- Exercise 10- Stars
2 |
3 |
4 | function showStars(rows) {
5 | let pattern = '';
6 | for (let i = 1; i <= rows; i++) {
7 | pattern += '*';
8 | console.log(pattern);
9 | }
10 | }
11 |
12 |
13 | //Mosh solution:
14 | function showStars(rows) {
15 | for (let row = 1; row <= rows; row++) {
16 | let pattern = '';
17 | for (let i = 0; i < row; i++) {
18 | pattern += '*';
19 | }
20 | console.log(pattern)
21 | }
22 | }
23 |
24 | showStars(5)
--------------------------------------------------------------------------------
/06 Arrays/09- The Spread Operator.js:
--------------------------------------------------------------------------------
1 | // 09- The Spread Operator
2 |
3 |
4 | /*
5 | With the Spread Operator we can also combine and copy arrays.
6 | This is a cleaner approach.
7 | */
8 |
9 | const first = [1, 2, 3];
10 | const second = [4, 5, 6];
11 |
12 | // Combining arrays
13 | const combined = [...first, ...second];
14 | console.log(combined);
15 |
16 | const combined2 = [...first, 'a', 'b', ...second, 'c'];
17 | console.log(combined2);
18 |
19 | // Copy array
20 |
21 | const copied = [...first];
22 | console.log(copied);
23 |
24 | const copied2 = [...combined];
25 | console.log(copied2)
--------------------------------------------------------------------------------
/07 Functions/12- Exercise 1- Sum of Arguments.js:
--------------------------------------------------------------------------------
1 | // 12- Exercise 1- Sum of Arguments
2 |
3 | function sum(...numbers) {
4 | if (Array.isArray(numbers[0])) {
5 | return numbers[0].reduce((acc, curr) => acc + curr);
6 | }
7 | return numbers.reduce((acc, curr) => acc + curr);
8 | }
9 |
10 | console.log(sum(1, 2, 3, 4, 5));
11 | console.log(sum([1, 2, 3, 4, 5]));
12 |
13 | // Mosh solution
14 |
15 | function sum(...items) {
16 | if (items.length ===1 && Array.isArray(items[0])) {
17 | items = [...items]
18 | }
19 | return numbers.reduce((acc, curr) => acc + curr);
20 | }
--------------------------------------------------------------------------------
/02 Basics/08- Types of Functions.js:
--------------------------------------------------------------------------------
1 | // 08 Types of Functions
2 |
3 |
4 | /*
5 | We have to types of functions:
6 | Performs a task
7 | Calculates value
8 | */
9 |
10 | // Performs a task
11 | function greetPerson(firstName, lastName) {
12 | console.log("Hello " + firstName + " " + lastName);
13 | }
14 |
15 |
16 | // Calculates value
17 | function square(number) {
18 | return number * number;
19 | }
20 |
21 | // This function is calculates a value and returning it. So we return the value to how ever is calling the function.
22 |
23 | let number = square(2)
24 | console.log(number)
--------------------------------------------------------------------------------
/07 Functions/14- Exercise 3- Error Handling.js:
--------------------------------------------------------------------------------
1 | // 14- Exercise 3- Error Handling
2 |
3 | function countOccurrences(array, searchElement) {
4 | if (!Array.isArray(array)) {
5 | throw new Error("First argument must be an array");
6 | }
7 | return array.reduce((accumulator, current) => {
8 | const occurrence = current === searchElement ? 1 : 0;
9 | return accumulator + occurrence;
10 | }, 0);
11 | }
12 |
13 | const numbers = [1, 2, 3, 4, 1, 5];
14 |
15 | try {
16 | const count = countOccurrences(null, 1);
17 | console.log(count);
18 | } catch (error) {
19 | console.error(error.message);
20 | }
21 |
--------------------------------------------------------------------------------
/05 Objects/04- Dynamic Nature of Objects.js:
--------------------------------------------------------------------------------
1 | // 04- Dynamic Nature of Objects
2 |
3 | /*
4 | In JavaScript object are dynamic, once they are created we can add new properties or methods to it.
5 | */
6 |
7 | const circle = {
8 | radius: 1,
9 | }
10 |
11 | circle.color = 'yellow'; // In here we add a new property to the object, in this case color
12 | circle.draw = function() { // Here we add a new method two the object.
13 | console.log('Draw')
14 | };
15 |
16 | console.log(circle);
17 |
18 | delete circle.color; // With the "delete" statement we can delete properties or methods.
19 |
20 | console.log(circle);
--------------------------------------------------------------------------------
/04 Control Flow/16- Exercise 7- String Properties.js:
--------------------------------------------------------------------------------
1 | // 16- Exercise 7- String Properties
2 |
3 | /*
4 | Create function "showProperties()"".
5 | Pass an object to that function, and it should and is should display all the properties of the object that are of type string.
6 | */
7 |
8 | function showProperties(object) {
9 | for (let property in object) {
10 | if (typeof object[property] === 'string')
11 | console.log(property, object[property]);
12 | }
13 | }
14 |
15 | const movie = {
16 | title: "a",
17 | releaseYear: 2018,
18 | rating: 4.5,
19 | director: "b"
20 | }
21 |
22 | showProperties(movie)
--------------------------------------------------------------------------------
/04 Control Flow/12- Exercise 3- FizzBuzz.js:
--------------------------------------------------------------------------------
1 | // 12- Exercise 3- FizzBuzz
2 |
3 |
4 | /*
5 | Create the FizzBuzz function
6 | Divisible by 3 => Fizz
7 | Divisible by 5 => Buzz
8 | Divisible by both 3 and 5 => FizzBuzz
9 | Not divisible by 3 or 5 => input
10 | Not a number => 'Not a number'
11 | */
12 |
13 | function fizzBuzz(input) {
14 | if (typeof input !== 'number') return NaN;
15 | else if ((input % 3 === 0) && (input % 5 === 0)) return 'FizzBuzz';
16 | else if (input % 3 === 0) return 'Fizz';
17 | else if (input % 5 === 0) return 'Buzz';
18 | else return input;
19 | }
20 |
21 | let result = fizzBuzz('a')
22 | console.log(result)
--------------------------------------------------------------------------------
/06 Arrays/18- Exercise 2- Includes.js:
--------------------------------------------------------------------------------
1 | // 18- Exercise 2- Includes
2 |
3 | function includes(array, searchElement) {
4 | let output = false;
5 | array.forEach(element => {
6 | if (element === searchElement) {
7 | output = true;
8 | }
9 | });
10 | return output;
11 | }
12 |
13 | const numbers = [1, 2, 3, 4, 5, 6];
14 |
15 | console.log(includes(numbers, -3));
16 |
17 | // Mosh solution
18 |
19 | function includesMosh(array, searchElement) {
20 | for (let element of array)
21 | if (element === searchElement)
22 | return true;
23 | return false;
24 | }
25 |
26 | console.log(includesMosh(numbers, 3));
--------------------------------------------------------------------------------
/03 Operators/04- Comparison Operators.js:
--------------------------------------------------------------------------------
1 | // 04- Comparison Operators
2 |
3 | /*
4 | An expression with a comparison operator will return a boolean value
5 | These are the comparison operators
6 | == equal to
7 | === equal value and equal type
8 | != not equal
9 | !== not equal value or not equal type
10 | > greater than
11 | < less than
12 | >= greater than or equal to
13 | <= less than or equal to
14 | */
15 |
16 | let x = 5;
17 |
18 | console.log(x == '5');
19 | console.log(x === 5);
20 | console.log(x != '5');
21 | console.log(x !== 5);
22 | console.log(x > 5);
23 | console.log(x < 5);
24 | console.log(x >= 5);
25 | console.log(x <= 5);
--------------------------------------------------------------------------------
/04 Control Flow/06- Infinite Loops.js:
--------------------------------------------------------------------------------
1 | // 06- Infinite Loops
2 |
3 | /*
4 | A infinite loop executes infinity or forever.
5 | If we create such a loop we may crash the browser or the computer.
6 | */
7 |
8 | let i = 0;
9 | while (i < 5){
10 | console.log(i);
11 | // i++
12 | }
13 | // If we forget to increment i we loop runs forever.
14 |
15 |
16 | for (i = 1; i > 0; i++) {
17 | console.log(i)
18 | }
19 | // This is an example of an infinity for loop, the condition i > 0 is always true, so the loop will never end.
20 |
21 | for (i = 1; i > 0;) {
22 | console.log(i)
23 | }
24 | // This is an example of an infinity for loop, because we are not incrementing i.
--------------------------------------------------------------------------------
/05 Objects/16- Exercise 2- Factory and Constructor Functions.js:
--------------------------------------------------------------------------------
1 | // 16- Exercise 2- Factory and Constructor Functions
2 |
3 | // Factory function
4 | function cretaAddress(street, city, zipCode) {
5 | return {
6 | street,
7 | city,
8 | zipCode
9 | };
10 | };
11 |
12 | // Constructor function
13 | function Address(street, city, zipCode) {
14 | this.street = street;
15 | this.city = city;
16 | this.zipCode = zipCode;
17 | };
18 |
19 |
20 | let address1 = cretaAddress('Rua Afonso Braz 505', 'São Paulo', '04511-011');
21 | console.log(address1);
22 |
23 | let address2 = new Address('Rua Augusta 2099', 'São Paulo', '01413-000');
24 | console.log(address2)
--------------------------------------------------------------------------------
/01 Getting Started/03- Setting Up the Development Environment.md:
--------------------------------------------------------------------------------
1 | # 03- Setting Up the Development Environment
2 |
3 | ## Code editor
4 |
5 | Using Visual Studio Code to write JavaScript.
6 |
7 | ## Node.js
8 |
9 | Download and install Node, We do not need Node to execute JavaScript, because can use the browser to execute JavaScript. But is is good to have node installed, because we use that to install third party libraries.
10 |
11 | ## Files
12 |
13 | Then we create a HTML file "index.html" that we are going to use as a host for the JavaScript code.
14 |
15 | ## VS Code Extension
16 |
17 | Then install a VS Code plugin Live Server, it a very light webserver that we are going to use to serve our application.
18 |
--------------------------------------------------------------------------------
/06 Arrays/14- Filtering an Array.js:
--------------------------------------------------------------------------------
1 | // 14- Filtering an Array
2 |
3 | /*
4 | To filter na array we can use the ".filter()" method.
5 | This method evaluates each element of the array with a given condition, and returns the elements that pass that condition to a new array.
6 | */
7 |
8 | const numbers = [1, 2, -1, 3, -4, -2, 4, 5, 6, 0];
9 |
10 | let filteredNumbers = numbers.filter(function(number) {
11 | return number > 0; // It will return the elements that pass this condition to a new array.
12 | });
13 |
14 | console.log(filteredNumbers);
15 |
16 | filteredNumbers = numbers.filter(number => number > 0); // It the same code as above but using arrow function for a cleaner code.
17 |
18 | console.log(filteredNumbers);
--------------------------------------------------------------------------------
/04 Control Flow/17- Exercise 8- Sum of Multiples of 3 and 5.js:
--------------------------------------------------------------------------------
1 | // 17- Exercise 8- Sum of Multiples of 3 and 5
2 |
3 | /*
4 | Create a function that will sum all the multiples of 3 and 5 from 0 the limit argument passed.
5 | */
6 |
7 | function sum(limit) {
8 | let result = 0;
9 | for (i = 0; i <= limit; i++) {
10 | if (i * 3 <= 10) result += (i * 3)
11 | if (i * 5 <= 10) result += (i * 5)
12 | }
13 | return result
14 | }
15 |
16 |
17 |
18 | // Mosh solution
19 | function sum(limit) {
20 | let sum = 0;
21 |
22 | for (let i = 0; i <= limit; i++) {
23 | if (i % 3 === 0 || i % 5 === 0)
24 | sum += i
25 | }
26 |
27 | return sum
28 | }
29 |
30 |
31 | console.log(sum(10))
--------------------------------------------------------------------------------
/04 Control Flow/15- Exercise 6- Count Truthy.js:
--------------------------------------------------------------------------------
1 | // 15- Exercise 6- Count Truthy
2 |
3 | /*
4 | Create function "countTruthy()"
5 | That will count truthy values in an array.
6 | */
7 |
8 | function countTruthy(array) {
9 | let counter = 0
10 | for (item of array) {
11 | if (item !== false && item !== undefined && item !== null && item !== 0 && item !== '' && item !== NaN)
12 | counter++;
13 | }
14 | return counter
15 | }
16 |
17 | const myArray = [1, 2, 0, 3, undefined, null, 5, 7, 'a', 'fsf']
18 |
19 | const result = countTruthy(myArray)
20 | console.log(result)
21 |
22 |
23 | // Mosh solution
24 | function countTruthy(array) {
25 | let count = 0
26 | for (let value of array) {
27 | if (value)
28 | count++;
29 | }
30 | return count
31 | }
--------------------------------------------------------------------------------
/07 Functions/02- Hoisting.js:
--------------------------------------------------------------------------------
1 | // 02 - Hoisting
2 |
3 | /*
4 | Hoisting is JavaScript's default behavior of moving declarations to the top.
5 | The key difference between Function Declaration and Function Expression is that:
6 | Function declarations can be called before being declared
7 | Function expression can not, it's the same as using a variable.
8 |
9 | The reason for this is when our JavaScript engine runs this code, it moves all function declarations to the top.
10 | Its called Hoisting. The process of moving Function Declarations to the top of the file, and it is made automatically by the Javascript engine.
11 | */
12 |
13 | walk();
14 |
15 | // Function declaration
16 | function walk() {
17 | console.log("walk");
18 | }
19 | // Anonymous Function Expression
20 | let run = function () {
21 | console.log("run");
22 | };
23 |
--------------------------------------------------------------------------------
/04 Control Flow/14- Exercise 5- Even and Odd Numbers.js:
--------------------------------------------------------------------------------
1 | // 14- Exercise 5- Even and Odd Numbers
2 |
3 |
4 | /*
5 | Create function "showNumbers()"
6 | Accepts a number as parameter, it will return all the numbers up to that number telling if it is odd or even
7 | Example:
8 | If we pass to that function 3 as argument, it should return the following.
9 | 0 "EVEN"
10 | 1 "ODD"
11 | 2 "EVEN"
12 | 3 "ODD"
13 | */
14 |
15 | function showNumbers(limit) {
16 | for (i = 0; i < limit + 1; i++) {
17 | if (i % 2 === 0) console.log(i , "EVEN")
18 | else console.log(i , "ODD")
19 | }
20 | }
21 |
22 |
23 | // Mosh solution for a cleaner code
24 | function showNumbers(limit) {
25 | for (i = 0; i <= limit; i++) {
26 | const message = (i % 2 === 0) ? "EVEN" : "ODD"
27 | console.log(i, message)
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/05 Objects/10- Garbage Collection.js:
--------------------------------------------------------------------------------
1 | // 10- Garbage Collection
2 |
3 | /*
4 | In low level languages like C or C++ when we create an object we have to allocate memory to it.
5 | And when we don't need to that ir anymore we have to deallocate the memory.
6 | In JavaScript we don't have this concept, when we create an object, memory is automatically allocated to it.
7 | And when we don~t need to use it anymore it is deallocate automatically.
8 | The JavaScript Engine has a Garbage Collector.
9 | The job of this Garbage Collector is to find the variables and constant that are no longer used, and then deallocate the memory that was allocated to then.
10 | Memory allocation and deallocation happens automatically behind the scenes, and we do not have control over it.
11 | */
12 |
13 | const x = {a: 1, b: 2};
14 | console.log(x);
15 | // Memory is allocated to constant "x" and automatically deallocated when not needed anymore.
16 |
--------------------------------------------------------------------------------
/06 Arrays/01- Introduction.js:
--------------------------------------------------------------------------------
1 | // 01- Introduction
2 |
3 | /*
4 | Description
5 | Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations.
6 | Neither the length of a JavaScript array nor the types of its elements are fixed.
7 |
8 | Most common operations we can perform in arrays:
9 | Adding elements
10 | Finding elements
11 | Removing elements
12 | Splitting arrays
13 | Combining arrays
14 |
15 | Constructor
16 | Array()
17 | Creates a new Array object.
18 | */
19 |
20 | // Initializing an array with the Constructor
21 | let myArray1 = new Array();
22 | console.log(myArray1);
23 |
24 | // Creating an array literal
25 | let myArray2 = [];
26 | console.log(myArray2);
27 |
28 | // Using "const"
29 | const myArray3 = [];
30 |
31 | // In case we use constant to create an array we can not reassign the variable.
32 | // But we can change the content of the array
--------------------------------------------------------------------------------
/06 Arrays/11- Joining Arrays.js:
--------------------------------------------------------------------------------
1 | // 11- Joining Arrays
2 |
3 | /*
4 | The "join()" method joins the elements array into a string.
5 |
6 | The join method takes one optional parameter, a string that will be used to join each item from the array.
7 |
8 | Syntax
9 | [].join()
10 | */
11 |
12 | const numbers = [1, 2, 3];
13 | const numbersJoined = numbers.join(';');
14 | console.log(numbersJoined);
15 | console.log(typeof numbersJoined); // The "joined()" method returns a string.
16 |
17 | // The "split()" method goes hand in hand With the "join()" method.
18 | // It converts a string to an array.
19 |
20 | const message = "This is a message";
21 | const messageArray = message.split(' ');
22 | console.log(messageArray);
23 |
24 | const urlSlugExample = messageArray.join('-');
25 | console.log(urlSlugExample);
26 | // This technic is particular useful to build url slugs.
27 | // For example when we want to convert a blog post title to the url.
--------------------------------------------------------------------------------
/03 Operators/06- Ternary Operator.js:
--------------------------------------------------------------------------------
1 | // 06- Ternary Operator
2 |
3 | /*
4 | The Ternary operator also know as conditional operator, assigns a value to a variable based on a condition.
5 | First we set a variable with a comparison operator, thats our condition.
6 | Them add a "?" followed by the the value if the condition is true, them after the ":" the value if the condition is false.
7 | let variable name = (condition) ? value1 : value2
8 | let voteable = (age < 18) ? "Too young" : "Old enough";
9 | Example explained: If the variable "age" is a value below 18, the value of the variable "voteable" will be "Too young", otherwise the value of voteable will be "Old enough".
10 | */
11 |
12 | /*
13 | If a costumer has more than 100 points
14 | They are "gold" costumer, otherwise they are silver consumer
15 | */
16 |
17 | let costumerPoints = 110;
18 | let typeCostumer = costumerPoints > 100 ? 'gold' : 'silver'
19 | console.log(typeCostumer)
--------------------------------------------------------------------------------
/05 Objects/11- Math.js:
--------------------------------------------------------------------------------
1 | // 11- Math
2 |
3 | /*
4 | Math object
5 | Is one of the built-in objects in JavaScript.
6 | https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math
7 |
8 | Reviewing a few properties and methods from the "Math" object.
9 | */
10 |
11 | // Pi property
12 | const pi = Math.PI
13 | console.log(pi)
14 |
15 |
16 | // Random method
17 | console.log(Math.random())
18 | // The random method returns a random number between 0 and 1. In case we want to specify the range we can use a function.
19 | function getRandom(min, max) {
20 | return Math.random() * (max - min) + min;
21 | };
22 |
23 | // Round method
24 | console.log(Math.round(1.954));
25 |
26 | // Max method
27 | console.log(Math.max(1, 2, 5, 4, 7, 9, 6));
28 | // Pass to it several numbers and it will return the max.
29 |
30 | // Min method
31 | console.log(Math.min(1, 2, 5, 4, 7, 9, 6));
32 | // Pass to it several numbers and it will return the min.
33 |
--------------------------------------------------------------------------------
/05 Objects/08- Enumerating Properties of an Object.js:
--------------------------------------------------------------------------------
1 | // 08- Enumerating Properties of an Object
2 |
3 |
4 | /*
5 | There are different ways to iterate over the properties of an object.
6 | We saw the for loops
7 | */
8 |
9 | const circle = {
10 | radius: 1,
11 | draw() {
12 | console.log('Draw');
13 | }
14 | }
15 |
16 | // Using the for..in loop to get the properties and values of an object
17 | for (let key in circle) {
18 | console.log(key, circle[key]);
19 | }
20 |
21 | for (let key of Object.keys(circle)) { // The "Object.keys()" method will return an array of strings with the object properties.
22 | console.log(key);
23 | }
24 |
25 | for (let entry of Object.entries(circle)) { // The "Object.entries()" method will return an array with the object key value pair.
26 | console.log(entry);
27 | }
28 |
29 | if ('radius' in circle) console.log('Yes') // With the in operator we can check if a given property exists in an object.
--------------------------------------------------------------------------------
/07 Functions/03- Arguments.js:
--------------------------------------------------------------------------------
1 | // 03 - Arguments
2 |
3 | /*
4 | JavaScript is dynamic language
5 | He have the same concept in the arguments of a function
6 | */
7 |
8 | function sum(a, b) {
9 | return a + b;
10 | }
11 |
12 | console.log(sum(1, 2)); // This will return 3 as expected
13 | console.log(sum(1)); // This will return NaN (Not a Number). Because the function is returning 1 + undefined
14 | console.log(sum()); // This will return NaN (Not a Number). Because the function is returning undefined + undefined
15 | console.log(sum(1, 2, 3, 4, 5)); // This will return 3. Only the first two arguments are used, the rest is being ignored.
16 | // The point is we do not get an error.
17 |
18 | // If want to have the flexibility to pass as many arguments as we want.
19 | // Every function in JavaScript as a special object called arguments
20 |
21 | function sum2() {
22 | let total = 0;
23 | for (let num of arguments) {
24 | total += num;
25 | }
26 | return total;
27 | }
28 |
29 | console.log(sum2(1, 2, 3, 4, 5));
--------------------------------------------------------------------------------
/06 Arrays/06- Removing Elements.js:
--------------------------------------------------------------------------------
1 | // 06- Removing Elements
2 |
3 |
4 | const numbers = [1, 2, 3, 4, 5, 6]
5 |
6 | // Remove the last element
7 | // The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
8 | const last = numbers.pop();
9 | console.log(last);
10 | console.log(numbers);
11 |
12 | // Remove the first element
13 | // The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.
14 | const first = numbers.shift();
15 | console.log(first);
16 | console.log(numbers);
17 |
18 | // Remove one or more elements from the middle
19 | // The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
20 |
21 |
22 | const elementsRemoved = numbers.splice(1, 2); // With the "splice()", it will remove n elements counting from the indicated index.
23 | console.log(elementsRemoved);
24 | console.log(numbers);
25 |
--------------------------------------------------------------------------------
/06 Arrays/20- Exercise 4- Moving an Element.js:
--------------------------------------------------------------------------------
1 | // 20 - Exercise 4 - Moving an Element
2 |
3 | const numbers = [1, 2, 3, 4];
4 |
5 | const output = move(numbers, 0, 4);
6 | console.log(output);
7 | console.log(numbers);
8 |
9 | function move(array, index, offset) {
10 | const output = [...array];
11 | const itemToMove = output.splice(index, 1)[0];
12 | const positionToInsert = index + offset;
13 | if (positionToInsert >= 0 && positionToInsert <= numbers.length) {
14 | output.splice(positionToInsert, 0, itemToMove);
15 | return output;
16 | } else {
17 | console.error('Invalid offset');
18 | }
19 |
20 | }
21 |
22 |
23 | // Mosh solution
24 |
25 | function moveMosh(array, index, offset) {
26 | const position = index + offset;
27 | if (position >= array.length || position < 0) {
28 | console.error('Invalid offset');
29 | return;
30 | }
31 | const output = [...array];
32 | const element = output.splice(index, 1)[0];
33 | output.splice(position, 0, element);
34 | return output;
35 | }
36 |
--------------------------------------------------------------------------------
/01 Getting Started/05- Separation of Concerns.md:
--------------------------------------------------------------------------------
1 | # 05- Separation of Concerns
2 |
3 | Although we can write the JavaScript code in the `
24 |