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’:
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 since we start with 0, not 1!) to add up the numbers and get your 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), 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.