Tuesday 3 May 2016

for & while loops - The Complete Node JS Developer Course

for and while loops


Lesson 19 started off with a simple demonstration. We followed along and created the simple countdown program below.



Once that was done, we had to go away and tackle a little project of our own. This time, we had to create a couple of countdown functions, that both do the same thing, but one uses a while loop, and the other uses a for loop. Here's mine below:


After that, I resumed the video and followed the tutor's worked example. See below:


I've got loads of notes on for and while loops from the codecademy course that I did a few months ago, so I'll write a full blog on that soon.

Sunday 1 May 2016

PROJECT 2 - The Complete Node JS Developer Course



In this project we want to be able to do the following things:

  1. Create new bank accounts
  2. Search for bank accounts
  3. Deposit and withdraw amounts
  4. Check account balances

In lesson 18 of The Complete Node JS Developer Course (check here for an update on my progress so far), the task was set out, and we had to go away and work through it independently. Lesson 19 is a step-by-step guide to a complete solution.

Getting Started


In lesson 18 we did the ground work for the project. This is the code we created to set the foundations:



We have created an empty array that will store all of the accounts, and there are some methods (or functions) that we can call on to make deposits, withdrawals, and check balances.

The Task


In the instructions we are told to create a new function called 'createAccount'. It's going to take an account object and store it in an array of accounts.

Each account object needs a 'balance', which should be a number, and a 'username', which should be a string.

The second function we're going to create is called 'getAccount' and takes one argument, which is 'username'.

The Solution


createAccount

We start with the 'createAccount' function. It accepts one argument, which is the object 'account'.


Basically, what we've done there is create a function, 'createAccount', that takes an account object and pushes it into the accounts array.

Now we can work on our 'getAccount' function.

getAccount


Within the function, we use forEach to iterate over the accounts and find any matching account.

We create a new variable called 'matchedAccount' within the function, which will store the account if a match is found.

Within the forEach, we want to check if the username of the account matches any of our accounts, so we create an if statement to do that. We then save the account to the matchedAccount variable that we created earlier.

We then return the matched account.

Re-cap


So, here's what we have so far:


Underneath that, we are going to create a new account.

New Account


We create a new account and save it to a variable. We then set that variable equal to createAccount, which is a function that expects an account object. The new object is created right within the function! Awesome! We give it a username and a balance. Yay!


Let's start as we mean to go on, and make a deposit into our account.

Deposit


We'll be using that function we created earlier on lines 19 to 21. Here's a quick reminder:


Here's how we make a deposit into our new account:


Lovely!

We use console.log to keep track of things, and we do this with the getBalance function (also created earlier on lines 27 to 29):


Withdrawal


Another old'un, but a good'un (on lines 23 to 25):


And here's how we use it:


getBalance makes another appearance :)

getAccount

The star of the show!

Create a new variable called existingAccount, and set it equal to getAccount, with the username passed in. It accepts one argument, a string.


It will return the balance of the my account!

We can test it out by creating another account for Pinky.


To make sure that everything is working as it should do, we can log out both of the accounts that we have created so far, by simply doing the following:


That will log them both out to the terminal, with their usernames and balances. Beautiful!

Finally, let's create a new variable for the Pinky's account (like we did with my account earlier):


Here's a re-cap of that code from line 31 onwards:


And it's all done! Until the next time :)

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.

Tuesday 26 April 2016

The Complete Node JS Developer Course - Variables, Error Messages & Strings

Variables


Lesson 8 is an introduction to variables.

A great thing about this course, and something that would have been extremely useful when I was doing the codecademy JavaScript course, is the brief introduction to error messages. This is vital knowledge for people who want to work in the field of programming.

Jumping ahead slightly, here's a quick insight from lesson 9:

It's common to see variable names that start with a lower case letter. A useful thing to know is that the first character in the name of a variable can also be an upper case letter, an underscore _ or a dollar sign $. The subsequent letters of a variable can be lower or upper case letters, or numbers, i.e. from 0 to 9.

Error Messages


Andrew shows you the right way to create a simple variable, using the var key word, and he explains that var is used to signify a variable, but it cannot be used to name a variable. In order to explain that concept, he gets us to deliberately create a variable with the name var so we can see the error message that it throws up.

This course is getting a big thumbs up from me so far! :)

Strings


In lesson 9 we learn how to identify and create a string.


Usually programmers use a single quotation for strings, because it doesn't require any thinking. You just tap the key on the keyboard and voila! But you could use double quotes if you wanted, but that would require pressing the shift key as well, so it's more to think about.

It's best practice to use the same type of quotation marks consistently throughout your code, so programmers tend to choose the single quotes as it's the most instinctive option.

This is another helpful insight from Andrew as he really gets into the psyche of the newbie.

The Complete Node JS Developer Course @Udemy

 

The Complete Node JS Developer Course @Udemy


This Udemy course was recommended to me by Kingsley Ijomah. He's an experienced software engineer and a fantastic online coding tutor! He has not yet produced a JavaScript or Node.js course, but he keeps his ear to the ground.

The main instructor for this course is Andrew Mead.


Section 1

Course Intro


The first three videos are to introduce the course and the instructor, and to get you all set up and ready to take the course. First we downloaded Node.js and the recommended text editor, Sublime Text, which I have been using for about four or five years.

Section 2

Introduction to Terminal


We're going to be using the terminal to run our Node.js files. I had some lessons on the Terminal about a year ago, and it reminds me of using my mum's old computer when I was a kid. I just got used to it because I used it every day to play games on floppy disk... Fast forward a few years, and I have unlearned a lot of stuff! The more you use Terminal, the more instinctual it becomes.

I use a mac, so I followed lesson 4, which is specifically for OS X & Linux. Lesson 5 is for Microsoft.

In this lesson we're learning to navigate our computer using the Terminal. Here are the main prompts we've covered so far:

pwd = print working directory
ls  Lists out all the folders and files inside the current folder
cd = change directory - Allows you to specify a folder name and navigate to that folder

The instructor, Andrew Mead, runs at a really good pace, and it's easy to keep up with the course. So far, so awesome.

Hello World!


In lesson 6 we learn that to run a node file from the Terminal, you must type the following:

node filename.js

So, that's the word node, followed by a space, followed by the .js file name and then press enter.

This lesson was really slow and simple. The task was to create a .js file, write a simple console.log statement containing a string, and run it from the Terminal.

For someone who knows JavaScript already, parts of the course may be a little slow and repetitive, but that's great for people who want to learn or revise JavaScript and Node.js at the same time.

Section 3

What is Node.js?


In lesson 7 we're back to the Terminal again. If you simply type the word node and press enter, you can use the Terminal as your text editor instead of using Sublime.

Section 3 of this course is pretty much teaching you JavaScript.

Node.js is essentially JavaScript, but it can do some things that JavaScript can't. The differences are explained in more detail later on in the course.