Friday, 29 April 2016

JavaScript Revision Notes - Functions

Here are some flashcards that will be helpful when starting out your JavaScript journey:

Functions


Here's how to create a simple function:


In our example, the parentheses are empty.


But we can also add arguments to our function.

We do this by defining the arguments inside the parentheses, and then adding them to the function when we call it:


We have defined the argument inside the parenthesis, giving it the name 'name', and we have passed in a string value of 'Danielle' when we call it. This will print out 'Hello Danielle'.

Arrays - The Complete Node JS Developer Course

Arrays


Lesson 17 covers some really interesting stuff!

Methods


A method is nothing more than a function; it's just accessed using dot notation.

.push method


You can add an item to the end of an array using the .push method!


.unshift method


Here, we have an array containing the numbers 100 and 50. But by using the .push method, we have added the number 79 to the end of the array. So when we run it from the Terminal, it will return all three numbers: 100, 50, 79.

You can add an item to the beginning of an array by using the .unshift method.


.pop method


You can also remove items from an array. The .pop method removes the last item of the array. So when you run the program, that last item will no longer be returned, but the next item along will be returned instead.


In this case, the .pop method on line 3 will remove 79 from the end of the array. Line 5 will show us which item has been removed; i.e. 79 will be logged out by line 5. By the time we get to line 6 and log out the full array, the number 79 will be nowhere to be seen! .pop magic!

.shift method


As we saw in the last lesson, the .pop method removes things from the end of an array. The .shift method, on the other hand, removes the first item of an array.


In the example above, 100 will be removed by line 3, logged out by line 5, and nowhere to be seen when line 6 logs out the full array.

Iteration: the forEach method


This method is special because it takes a function as an argument.


Functions


In JavaScript, naming a function is not required. It's only required if you're going to be referencing the function at any given point. If you're simply passing in the function as an argument, there's no need to name it.

.length method


In this example, the .length method will log out the length of the array, which is 3, because there are 3 items in the array.


The Challenge


We had to create a program that added the grades together (i.e. 100 + 50 + 79), divided that total by the number of grades (i.e. 229 / 3) and returned the result (i.e. 76.333...). It looked a little something like this:



Undefined - The Complete Node JS Developer Course

Undefined


Lesson 16 of The Complete Node JS Developer course @Udemy is all about undefined. This independent task follows on from the previous lessons, and includes functions and if statements. Typeof also makes a reappearance.

This time we had to create a function that returns one thing if we give it an argument, or something else if we don't give it anything. See the code below. It is also on Github.


In the first instance, on line 16, we have given the function a name, Andrew, so it will return 'hello Andrew'. In the second case, on line 17, we have left the brackets empty, so it will return 'hello world'.

Thursday, 28 April 2016

Booleans - The Complete Node JS Developer Course

Booleans


Lesson 15 is all about Booleans! The taught part of the lesson was very clear and easy to follow; the independent task, however, was a bit vague, but the instructor did that deliberately. He wants us to get used to doing independent research and solving our own problems, because that will be an everyday part of life as a programmer. The internet has a wealth of forums and documentation that we need to get used to referencing.

I have uploaded the boolean task to GitHub. I did the task over two days, and here's the final product:


The task was to create a function that takes a boolean value and flips it. So if the value is false, the function will return true, and vice versa. Because I set the value of the variable isValid to false, the final outcome should be true.

The extra challenge was to create a function that only accepted booleans. That's where the bit of research came in, the fruit of which is on line 5. We hadn't come across typeof booleanVar in the course, so the journey continues to be very interesting and engaging.

As always, the instructor goes through everything and demonstrates a possible solution, so you're never completely stuck. I have modified my solution accordingly, because initially it was very lengthy, but now it's a little more elegant.

PROJECT 1 - Bank Account - The Complete Node JS Developer Course



10% of the course completed so far!

This course is great because towards the end of every lesson there's a little challenge to try out, so you can practice what was covered. You pause the video, give it a go, and then resume video to see Andrew, the instructor, demonstrate how he would have done it.

The PROJECT!

Lesson 14 is when we tackle our first complete project! We create a simple bank account, which ties together everything that we've learned so far about variables, objects and functions. We are going to be building on that project over the next few lessons, but check out the code below to see what we've done so far.


And here's what it looks like when we print to the Terminal.


The opening balance is printed out first, followed by the closing balance after the withdrawal, so it's all working great!

Objects - The Complete Node JS Developer Course

Lesson 12 is about functions and it is pretty straight forward to follow. The next lesson is about objects, and that's where things get really interesting!

Objects


Lesson 13 explains objects really well! As usual, the instructor gives useful and practical insights that will be needed in the field. For example, Andrew shows us how to log out a whole object via the terminal so we can see what values and properties are set in that object. That will be invaluable in future when debugging objects.

Bracket notation and dot notation are explained in detail in this lesson. I did not know that you could use bracket notation to create a property and then call the property using dot notation, and vice versa. That's good to know.

The main difference between bracket and dot notation, and the benefits of using bracket notation rather than dot notation in certain circumstances, are also explained.

Bracket notation is useful because you don't have to know the property name in order to access it, whereas with dot notation you do have to know the name.

Wednesday, 27 April 2016

The Complete Node JS Developer Course - Numbers & If statements

Numbers!


This is getting interesting! In lesson 10 we get to grips with using Maths operators.

We also learn a bit more about variables.

There are two ways to create variables, as illustrated below.

You can create a variable and 'initialize' it with a value, or you can create a variable for use later, and not give it a default value. Well I never! This is another great insider tip that will be needed in the field.



Great stuff!

Increment and Decrement

It can be easy to get disorientated when dealing with Mathematical operators. Here's another great insight that will help when incrementing and decrementing.

Example 1


The following will take the value 24 from the age variable and return 24 + 1, which is 25.


Look at line 5 in our example above, and compare it to the example below. That will perform the same calculation of 24 + 1, and achieve the same result of 25. It is, however, a much neater way of doing the same thing.


The following example will take the same value of 24 from the age value, but this time it will return 24 - 1, which is 23.


Again, if you look at the example below, we see the much neater way of doing the same thing.


When you're first confronted with age++ or age--, it can be intimidating, but when explained properly, it's quite simple really :)

Example 2


Another way to simplify things further is as follows:




So there we have it! Our journey into numbers and variables continues at a pace!

If statements


You can never get too much practice where if statements are concerned!

In lesson 11, because we haven't been introduced to arrays yet, there's lots of practice typing sets of data out the long way. But that's great, because you will need to use, ===!==&& and || in if statements (although not quite so intensively), so it's good to get to grips with what they do.

The course is great because you type along with the instructor as you go along, and then he sets little tasks so you can practice what's just been covered. Programming is such a practical skill so, in the early stages especially, it's a case of use it or lose it. We save each mini project into a file as we progress, so you can look back through the files and see how far you've come.