Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Wednesday, 2 March 2016

Node JS - Project 2 - Bank Account

Hello! I'm back again!

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 January 2016

Time to Celebrate! An Update on my JavaScript Journey



Back in December 2015, I set myself the challenge to Complete the Codecademy JavaScript course in one month (AND to code a binary search algorithm). When I finished the final lesson at 11pm on New Year's Eve it was brilliant! What better way to celebrate than by seeing in the new year! But I feel like I need to do something specifically to celebrate my achievement.

Rewards don't have to be synonymous with escapism


I could have a facial, or go out for a meal, but what has that got to do with programming? I feel like I should celebrate by using my new-found knowledge to create something.

A reward can be (and arguably should be) the furthering of a goal, so I have embarked on another challenge! It's taken me a month to figure out what to do (nothing like a bit of delayed gratification)! But now I have it! I've set myself another challenge: To create five JavaScript apps in five months! Wish me luck! :)

Sunday, 24 January 2016

Women: New World, New Technology. Own it!



Following on from the article, 7 Steps to Making Informed & Successful Career Choices, this piece is aimed at women in developing countries, but it could really apply to anyone who finds themselves at an economic disadvantage wherever they are. People who are looking for new ways to take control of their lives and their careers.

Local Conditions Matter - Especially if You Are a Woman!


If a woman in a developing country is poor, she will have to shoulder the majority of the domestic duties, whilst also having to contribute to the family income by doing menial jobs or trade and barter. She may have the same access to education as boys do at a young age, but she is more likely to drop out earlier on, due to domestic demands. She will have to face insurmountable odds to achieve the same things that I take for granted every day.

Men in developing nations may also have limited access to education, but they are able to go out to work, and not expected to help with domestic tasks. The types of jobs available to them - although still relatively menial - are wider, and being out of the home exposes them to wider networks, and wider career opportunities.

I have spoken to many women over the years who have shared their stories with me - their struggles, failures and successes - and the stories of others who were not so fortunate. Their stories highlight the importance of making wise and informed career choices, when as a woman it can be so easy to get caught up in the day-to-day of domestic duties and lose sight of the bigger picture - especially when no one sees your potential for greatness.

Your Choices Count More Than Ever When Faced With Limited Career Opportunities


There are very few skills and professions that international companies / organisations are likely to seek out beyond their own borders. There would have to be a distinct skills shortage in their domestic market for them to head-hunt abroad.

If a European or American company wants lawyers, doctors or accountants, they need not look any further than their domestic talent pool. Those roles, and others like them, are the 'prestige' careers that wealthy or middle class children pursue in order to add to their sense of achievement or their value to society. Ultimately, they are not faced with the hard realities and day-to-day struggles of financially challenged people in developing countries, so they can play the long game.

Pastures New


Poorer people in developing countries who aspire to those prestige professions may be missing the point. It may not be the ticket out of poverty that they were hoping for, and even if it is, they will not be able to stay in education long enough to make that dream a reality due to domestic demands and financial realities (unless they catch the eye of a rich relative, a charitable organisation or government scholarship).

The same goes for people in the West from disadvantaged socio-economic backgrounds. You could join the clamour for the prestige jobs, but unless you are exceptional, you will find yourself up against people who have been groomed for those careers from birth. The key is to beat a new path and carve out a niche.

Skills Beyond Borders


There are some skills, that are widely sought out by international organisations. They reward skill and natural ability, regardless of socio-economic or educational background. Sport can be one such ticket out of poverty, and into the extreme wealth that people dream of.

If you are a gifted young (male) footballer in a developing country, and you get into a football academy, you could find yourself playing for the likes of Man United or Arsenal one day. The key, it seems, for poor and uneducated people, is to choose a niche skill and be brilliant at it! The chances of success are a lot higher.

The only problem with the sport route, is that it favours men over women. Women are still not making the same amount of money as men from Football in Europe or baseball in America. The golden ticket that sport could offer is not an option for women. There are no scouts going to Brazil looking for female footballers.

Without education, women in developing countries, who want to make it big, have to find more creative ways to provide for their families.

Technology is a Great Leveller!


This is where technology comes in. Technology and programming skills are the key to a better life for many women (and men) around the world, and it is a realistic goal to pursue! Mobile phone usage in the developing world is high because of the lack of infrastructure, and internet access is also common - although people may have to go to an internet cafe to get it. So the barriers to entry are low.

The obvious routes out of poverty, that have had limited success for a limited few, no longer apply; but if you can master technology, and produce things of local or even international significance, you can name your price! Even if you just master programming skills without creating the next big app, there are so many companies in the UK that are actively encouraging applications from abroad! You only have to search the online jobs boards to see the opportunities available!

Take Control of Your Career


You don't need to be in a desperate situation to take control of your career. If you find that your prospects or your goals have stagnated, then do something about it. Don't be at the mercy of circumstances!

Technology is NOT the future!
It's here TODAY, and you can make it work for you!

The Next Steps


Learn programming and build your portfolio as you progress.

Online tutorials:

Codehance
Khan Academy
Codecademy

Wednesday, 9 December 2015

Programming. It's Less About Maths Than You Think - But maybe Maths is not what you think it is?



I really enjoyed Maths when I was at school. I liked puzzles and mysteries, and Maths is both of those things! I didn't pursue Maths beyond GCSE, but I have always enjoyed documentaries, programmes and books about the application of Maths to areas of science and discovery - such as astronomy, cartography, art, engineering, physics, and - you guessed it - computer science!

Eventually I was no longer content with just knowing about Maths, I wanted to 'do the Math'!

My Journey Into Numberland


After [many] years away from school I had done nothing more complex than working out prices, so I thought what better place to start than Maths GCSE... again! I did an intensive correspondence course in Higher Level GCSE Maths, and it was intense! (You can read more about it here)

Logical Thinking


Before you panic, I'm not suggesting that you go back to school and do Maths GCSE! You really don't need to. What struck me the most about doing the Maths course (as a mature student) was the realisation that Maths is mostly about thinking in a certain way. It is very logical. So, if you don't panic, you can work through the questions methodically and get the right answer. There are no surprises! You are not required to make discoveries! All you have to do is follow tried and tested steps!

Programming is the same in that respect, because it calls on existing resources and requires logical and organised thought. There's no reason why you couldn't go on to make amazing discoveries in the future, or invent something new, but you don't need to reinvent the wheel just yet.

You really don't need to be a Maths whizz to do programming! It's all about getting your thinking right.


When you are faced with a programming task / problem, if you can identify and understand the possible methods that could be used to solve it, then you are well on your way! It's all about getting your thinking right, and learning what the available resources are, as opposed to knowing everything in detail.

Programming Makes Maths Easy!


The great thing about programming is that most of the Mathematical methods for solving problems, such as performing searches on data, have already been turned into easily reusable code that you can pretty much copy and paste into your programme. Think calculators, but way cooler!

Knowing too much can sometimes be a disadvantage, and self-doubt can be very healthy in programming.


If you already know a lot, you may miss some important details and steam ahead, only to find that you've reached a dead end! If, however, you take a more analytical approach, by constantly asking questions as you go along, you will engineer and create a solid solution. So don't worry if you think you don't know enough. That's ok, and it could be an advantage!

Want to know more?


See my beginners' guide to Binary Search Algorithms for an insight to programmatic thinking and problem solving. I'll be blogging regularly about my journey through programming, and sharing as I learn new things.

Websites and Resources


Kingsley Ijomah
Khan Academy
Codecademy
Virtual Nerd




Please do leave a comment below if you have any questions. I'd love to hear from you!

Sunday, 6 December 2015

Getting from A to B with Algorithms... Don't Panic! Code On!



So, I set myself a bit of a crazy challenge a week ago. I decided to learn Javascript from scratch, and then use it to create a Binary Search Algorithm of a Sorted Array... Simples!

The challenge is from Khan Academy, and it assumes prior knowledge of Javascript. I wasn't going to let that little assumption get in my way! I started off with Codecademy - learned the basics, and then skipped straight to 'for' and 'while' loops.

I thought this would be a good topic for a blog article because I am relatively new to the practice of coding, and I wanted to share my experience with other budding beginners out there. If you are new to programming, or you would like to give programming a go, I hope you find this article interesting and useful.

So, what is an algorithm?


An algorithm is literally just a set of steps that you follow to get something done. Your daily routines are algorithms, for example, getting dressed, going to work, cooking a meal. That's not so intimidating is it!

The first step to coding an algorithm actually requires no coding at all. You just need to work out how to get from A to B. Sketch out the steps that need to be followed in order to get from A, which is your input, to B, which is the output (or result). Get a pen and paper, and start sketching out your ideas.

Plan your route before starting the journey!


Now, for experienced programmers, it is easy to jump straight in and start coding. The Khan academy tutorial has the task written out in plain English, with a tiny bit of pseudocode, so they've done all the thinking for you right? Wrong! It is still best practice to map out your own process. That way you can ask questions and get a complete understanding of the task at hand. But if you're ok with buggy code, go right ahead!

I'm going to show you how I did my planning, and then I will share my code at the end.

The Task


We have an array of prime numbers from 2 to 97.


Feed a random number into the binary search algorithm.
If the number is present in the array, the programme will return the index position of that number.
If the number is not present, the programme will return '-1'.


Binary Search


Binary Search is a really efficient way to find an item in an ordered list of data. In this case we have the list of prime numbers above. The search is a process of elimination. You narrow down the possible options by dividing the data in half over and over again, eliminating all the possible options until you are left with just one, which will be target.

Our minimum value is 2, which is at minimum index position 0.
Our maximum value is 97, which is at maximum index position 24.

Note: Because our array starts from index position 0, and ends at position 24, that means there are 25 values in the array.

How it works


To illustrate how this works, I am going to choose a random number and show how the algorithm will 'guess' my number.

Step 1


The programme starts by guessing the mid-point of our array, which is index position 12 in this case.

The algorithm establishes the mid-point in the following way:

My number = 67


Is my number equal to the first guess, i.e. is 67 = 41? No. My number is 67, is at position 18, and the first guess was 41, which is at position 12.

Step 2 & 3


We want the programme to compare the value of our number, 67, and the value held at index position 12, which is 41.

If 67 is greater than 41, we know we can eliminate all the values to the left of and including position 12 in the array, because they are all less than 41, which in turn is less than 67.

If the our number had been less than 41, we would do the opposite and eliminate the values to the right of and including position 12.


Step 4



Step 5


We now need to find our new mid-point, so we repeat Step 1 as follows:


We have arrived at our number!

This programme will keep running until it finds our number.

But what if we choose a number that is not present in the array?

We simply say to the programme, if the number we choose does not match any of the values in the array, it will give us an output to indicate that. In this case, I have asked the programme to return the value '-1'.

Edge Cases


Something I will touch on very briefly is Edge Cases. The programme that I have written above does not factor in what happens if I were to choose the values held at index position 0 or 24, which are the prime numbers 2 and 97. They are what is referred to as 'edge cases'. The values at the edges of an array.

So, I had to write a couple of extra lines of code to deal with them. I gave the programme specific instructions about what to do if I choose either number 2 or 97 as my random numbers. As with all programming, there are different ways to do everything. I will not go into detail, but here is my solution:

If my number is 2, return the value: index minimum;
but if my number is 97, return the value index maximum.

The Code



The Main Challenges


  1. Getting to grips with for loops and while loops required a lot of repetition. I completed the exercises on Codecademy and revisited them a few times to make sure that I fully understood the flow.
  2. The code performed really well when testing the mid points of the array, but some extra code was needed to test the edge cases.
  3. Although the code is neat and tidy, it could do with a bit of refactoring, so that's my task for another day!

Long story short, I finished it yesterday! Hooray!


It was a really fun process because it's like solving a puzzle. Who needs sudoku when you can code!

If you are thinking about coding, stop! Don't think about it anymore. Just get started!!! Codecademy is a great resource to start with, or you can check out these amazing tutorials from +Kingsley Ijomah He starts with html & css, moves on to Bootstrap, and then Ruby on Rails. The great thing about his tutorials is that he assumes that you have absolutely no prior knowledge of coding. The tutorials are for absolute beginners! Get started today!

About Me


I am not a programmer, I am a dabbler, with a curious interest in Mathematics. I have not worked commercially as a programmer, but I co-founded a software company back in 2008, and I have worked with programmers since then. The bulk of my experience is with html and css, but I have studied Ruby and a bit of Python, so I understand the key concepts and I can hold my own. I have recently made a foray into the world of MVC (Ruby on Rails), which is a whole new experience, but I did not have a project that I had coded from start to finish before, until now! So when I did my first git commit yesterday, it felt good!!!