# FreeCodeCamp and Learning Node: Baby Steps

So, after completing the Front End Certificate for FreeCodeCamp, the next task is learning to do the Back End!  FreeCodeCamp includes sections on Git, Node.JS, Express.js, and MondoDB.

Long ago, I started the Node.js (via http://nodeschool.io/) learnyounode tutorial and was immediately lost.  That was back when the FreeCodeCamp curriculum wasn’t as extensively developed as it is today.  This is my second go at it, and I find that writing the programs is MUCH easier now.  The Node commands themselves are still a little mystifying, and so I decided that, why not, I will blog about my experiences again.

Now, as I’ve said before, if you haven’t tried these tutorials yourself, STOP READING AND GO TRY THEM FIRST!  Again, reading my solution may help you do the lesson and move on, but you won’t learn anything.  If you have done it, and my solution looks totally different than yours, tell me because I want to learn new things also!  I’m no expert, just a student making his way through the world of coding!

I’m going to skip the first exercise, ‘Hello World’, except to note that I didn’t find the Cloud9 interface very beginner-friendly.

Here are the instructions for the next exercise, ‘Baby Steps’:

## BABY STEPS (Exercise 2 of 13)
Write a program that accepts one or more numbers as command-line arguments and prints the sum of those numbers to the console (stdout).
─────────────────────────────────────────────────────────────────────────────
## HINTS
You can access command-line arguments via the global process object. The process object has an argv property which is an array containing the complete command-line. i.e. process.argv.
To get started, write a program that simply contains:
console.log(process.argv)
Run it with node program.js and some numbers as arguments. e.g:
\$ node program.js 1 2 3
In which case the output would be an array looking something like:
[ ‘node’, ‘/path/to/your/program.js’, ‘1’, ‘2’, ‘3’ ]
You’ll need to think about how to loop through the number arguments so you can output just their sum. The first element of the process.argv array is always ‘node’, and the second element is always the path to your program.js file, so you need to start at the 3rd element (index 2), adding each item to the total until you reach the end of the array.
Also be aware that all elements of process.argv are strings and you may need to coerce them into numbers. You can do this by prefixing the property with + or passing it to Number(). e.g. +process.argv[2] or Number(process.argv[2]).

learnyounode will be supplying arguments to your program when you run learnyounode verify program.js so you don’t need to supply them yourself.  To test your program without verifying it, you can invoke it with learnyounode run program.js. When you use run, you are invoking the test environment that learnyounode sets up for each exercise.

Okay, so that was clear…as mud, at least it was to me, the first time I read it.  For an experienced programmer, I’m sure that it looked easy no problem.  But that’s not me (yet) and maybe it’s not you either, which is why you are here reading this!

Now, the assignment itself is easy – take the arguments, print out the sum.  We’ve done stuff like this back in the Front End exercises for FreeCodeCamp, and we’re going to use our old friend,l the argument object, which I wrote about in this post.  They call the arguments object process.argv here.  Also, in Node, the arguments now have some extra stuff besides numbers – namely, the word ‘node’, and the path to your program (that’s the location of it in the directory) as the the first two elements of the arguments object.  So you have to start with the third element in the arguments object (remember, that would be process.argv[2] since we start with 0, not 1!) to add up the numbers and get your sum.

Okay, so create a new file – I do it by clicking on the little green plus sign up there in the row of tabs – and we’ll start banging out the program.  This is actually a very simple JavaScript program once you get past all the verbiage above.

var sum = 0;
for(var i = 2; i < process.argv.length; i++) {
sum += Number(process.argv[i]);
}

console.log(sum);

We create a variable to actually hold the sum.  Now, we know that we are adding a bunch of numbers together that are being given to us in the arguments object, so we have to iterate through them.  That calls for a for loop.  Remember, we are starting with the third element (element[2]), so our loop is going to begin with 2, not 0.  Then we just just keep updating sum by adding the next value to it.  We stop when we reach the end of the arguments.  Finally, we print the sum to the console.

Make sure you save your file, or you’ll get nothing.  Also, if you click on the run button at the top of the screen, you’ll get nothing, especially if you haven’t named your file.  You can save with control-S (I’m using a Windows machine, obviously) or you can click on the file menu and select save as. I’ve attached a graphic below to show some of these things.

To run, we enter learnyounode verify program.js at the command line (down by the \$ sign).  Mind you, if you’ve named your program anything but program.js, you should put the name you called it in that command instead.